Fixed detached files discovery

Searching for detached files used PostSearchService, which by default
hides all hidden posts unless user explicitly asks to search for them.
That way, all hidden posts were treated as detached, so their content
was being removed when using the script in question.
This commit is contained in:
Marcin Kurczewski 2014-06-13 12:03:59 +02:00
parent a945a1d6db
commit d45da1e0ae
3 changed files with 17 additions and 1 deletions

View file

@ -60,7 +60,7 @@ switch ($action)
}
$names = [];
foreach (PostSearchService::getEntities(null, null, null) as $post)
foreach (PostModel::getAll() as $post)
{
$names []= $post->getName();
}

View file

@ -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();

View file

@ -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');
}