diff --git a/scripts/process-detached-files.php b/scripts/process-detached-files.php index fc0e74e7..c86e4e19 100644 --- a/scripts/process-detached-files.php +++ b/scripts/process-detached-files.php @@ -60,7 +60,7 @@ switch ($action) } $names = []; -foreach (PostSearchService::getEntities(null, null, null) as $post) +foreach (PostModel::getAll() as $post) { $names []= $post->getName(); } diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 8b6a6a01..3de54983 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -104,6 +104,15 @@ abstract class AbstractCrudModel implements IModel : null; } + public static function getAll() + { + $stmt = Sql\Statements::select(); + $stmt->setColumn('*'); + $stmt->setTable(static::getTableName()); + $rows = Core::getDatabase()->fetchAll($stmt); + return static::spawnFromDatabaseRows($rows); + } + public static function getAllByIds(array $ids) { $stmt = Sql\Statements::select(); diff --git a/tests/Tests/MiscTests/ScriptTest.php b/tests/Tests/MiscTests/ScriptTest.php index 797477b8..e514c249 100644 --- a/tests/Tests/MiscTests/ScriptTest.php +++ b/tests/Tests/MiscTests/ScriptTest.php @@ -46,14 +46,18 @@ class ScriptTest extends AbstractTest public function testDetachedFilesPrint() { $post = $this->postMocker->mockSingle(); + $post->setHidden(true); + PostModel::save($post); touch(Core::getConfig()->main->filesPath . DS . 'rubbish1'); touch(Core::getConfig()->main->filesPath . DS . 'rubbish2'); + touch(Core::getConfig()->main->filesPath . DS . $post->getName()); $output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-p']); $this->assert->isTrue(strpos($output, 'rubbish1') !== false); $this->assert->isTrue(strpos($output, 'rubbish2') !== false); $this->assert->isFalse(strpos($output, $post->getName())); $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish1')); $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish2')); + $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . $post->getName())); } public function testDetachedFilesRemove() @@ -67,6 +71,7 @@ class ScriptTest extends AbstractTest $this->assert->isFalse(strpos($output, $post->getName())); $this->assert->isFalse(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish1')); $this->assert->isFalse(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish2')); + $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . $post->getName())); } public function testDetachedFilesMove() @@ -81,6 +86,8 @@ class ScriptTest extends AbstractTest $this->assert->isFalse(strpos($output, $post->getName())); $this->assert->isTrue(file_exists($target . DS . 'rubbish1')); $this->assert->isTrue(file_exists($target . DS . 'rubbish2')); + $this->assert->isFalse(file_exists($target . DS . DS . $post->getName())); + $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . $post->getName())); unlink($target . DS . 'rubbish1'); unlink($target . DS . 'rubbish2'); }