Added script unit tests

This commit is contained in:
Marcin Kurczewski 2014-06-01 13:41:08 +02:00
parent 4d13a83047
commit 4ec0df17d6
6 changed files with 129 additions and 71 deletions

View file

@ -4,8 +4,8 @@ require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
array_shift($argv);
$query = join(' ', $argv);
$query = array_shift($argv);
$posts = PostSearchService::getEntities($query, null, null);
foreach ($posts as $post)
{

View file

@ -3,13 +3,19 @@ require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
$options = getopt('f', ['force']);
$force = (isset($options['f']) or isset($options['force']));
$query = [];
$force = false;
$args = array_search('--', $argv);
$args = array_splice($argv, $args ? ++$args : (count($argv) - count($options)));
array_shift($argv);
foreach ($argv as $arg)
{
if ($arg == '-f' or $arg == '--force')
$force = true;
else
$query []= $arg;
}
$query = join(' ', $query);
$query = array_shift($args);
$posts = PostSearchService::getEntities($query, null, null);
$entityCount = PostSearchService::getEntityCount($query, null, null);
$i = 0;

View file

@ -3,30 +3,34 @@ require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
function usage()
$usage = function()
{
echo 'Usage: ' . basename(__FILE__);
echo ' -print|-purge|-move DIR' . PHP_EOL;
echo 'Usage: ' . basename(__FILE__) . PHP_EOL;
echo ' -p|--print OR' . PHP_EOL;
echo ' -d|--delete OR' . PHP_EOL;
echo ' -m|--move [TARGET]' . PHP_EOL;
return true;
}
};
array_shift($argv);
if (empty($argv))
usage() and die;
$usage() and die;
$action = array_shift($argv);
switch ($action)
{
case '-print':
case '-p':
case '--print':
$func = function($name)
{
echo $name . PHP_EOL;
};
break;
case '-move':
case '-m':
case '--move':
if (empty($argv))
usage() and die;
$usage() and die;
$dir = array_shift($argv);
if (!file_exists($dir))
mkdir($dir, 0755, true);
@ -41,7 +45,8 @@ switch ($action)
};
break;
case '-purge':
case '-d':
case '--delete':
$func = function($name)
{
echo $name . PHP_EOL;

View file

@ -1,54 +0,0 @@
<?php
require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
function usage()
{
echo 'Usage: ' . basename(__FILE__);
echo ' -print|-purge';
return true;
}
array_shift($argv);
if (empty($argv))
usage() and die;
function printUser($user)
{
echo 'ID: ' . $user->getId() . PHP_EOL;
echo 'Name: ' . $user->getName() . PHP_EOL;
echo 'E-mail: ' . $user->getUnconfirmedEmail() . PHP_EOL;
echo 'Date joined: ' . date('Y-m-d H:i:s', $user->getJoinTime()) . PHP_EOL;
echo PHP_EOL;
}
$action = array_shift($argv);
switch ($action)
{
case '-print':
$func = 'printUser';
break;
case '-purge':
$func = function($user)
{
printUser($user);
UserModel::remove($user);
};
break;
default:
die('Unknown action' . PHP_EOL);
}
$users = UserSearchService::getEntities(null, null, null);
foreach ($users as $user)
{
if (!$user->getConfirmedEmail()
and !$user->getLastLoginTime()
and ((time() - $user->getJoinTime()) > 21 * 24 * 60 * 60))
{
$func($user);
}
}

View file

@ -1,12 +1,14 @@
<?php
class Access
{
private static $privileges = [];
private static $checkPrivileges = true;
private static $privileges;
private static $checkPrivileges;
public static function init()
{
self::$privileges = [];
self::$checkPrivileges = true;
foreach (Core::getConfig()->privileges as $key => $minAccessRankName)
{
if (strpos($key, '.') === false)

View file

@ -0,0 +1,99 @@
<?php
class ScriptTest extends AbstractTest
{
private $scriptsPath;
public function __construct()
{
parent::__construct();
$this->scriptsPath = Core::getConfig()->rootDir . DS . 'scripts' . DS;
}
public function testFindPosts()
{
$posts = $this->postMocker->mockMultiple(3);
$output = $this->execute($this->scriptsPath . 'find-posts.php', []);
$this->assert->isTrue(strpos($output, $posts[0]->getName()) !== false);
$this->assert->isTrue(strpos($output, $posts[1]->getName()) !== false);
$this->assert->isTrue(strpos($output, $posts[2]->getName()) !== false);
}
public function testFindPostsFilter()
{
$posts = $this->postMocker->mockMultiple(3);
$output = $this->execute($this->scriptsPath . 'find-posts.php', ['idmin:' . $posts[1]->getId()]);
$this->assert->isTrue(strpos($output, $posts[0]->getName()) === false);
$this->assert->isTrue(strpos($output, $posts[1]->getName()) !== false);
$this->assert->isTrue(strpos($output, $posts[2]->getName()) !== false);
}
public function testGenerateThumbs()
{
$posts = $this->postMocker->mockMultiple(3);
$this->assert->isFalse(file_exists($posts[0]->getThumbnailPath()));
$this->assert->isFalse(file_exists($posts[1]->getThumbnailPath()));
$this->assert->isFalse(file_exists($posts[2]->getThumbnailPath()));
$output = $this->execute($this->scriptsPath . 'generate-thumbs.php', []);
$this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[0])) !== false);
$this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[1])) !== false);
$this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[2])) !== false);
$this->assert->isTrue(strpos($output, 'Don\'t forget to check access rights') !== false);
$this->assert->isTrue(file_exists($posts[0]->getThumbnailPath()));
$this->assert->isTrue(file_exists($posts[1]->getThumbnailPath()));
$this->assert->isTrue(file_exists($posts[2]->getThumbnailPath()));
}
public function testDetachedFilesPrint()
{
$post = $this->postMocker->mockSingle();
touch(Core::getConfig()->main->filesPath . DS . 'rubbish1');
touch(Core::getConfig()->main->filesPath . DS . 'rubbish2');
$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'));
}
public function testDetachedFilesRemove()
{
$post = $this->postMocker->mockSingle();
touch(Core::getConfig()->main->filesPath . DS . 'rubbish1');
touch(Core::getConfig()->main->filesPath . DS . 'rubbish2');
$output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-d']);
$this->assert->isTrue(strpos($output, 'rubbish1') !== false);
$this->assert->isTrue(strpos($output, 'rubbish2') !== false);
$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'));
}
public function testDetachedFilesMove()
{
$post = $this->postMocker->mockSingle();
touch(Core::getConfig()->main->filesPath . DS . 'rubbish1');
touch(Core::getConfig()->main->filesPath . DS . 'rubbish2');
$target = sys_get_temp_dir();
$output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-m', $target]);
$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($target . DS . 'rubbish1'));
$this->assert->isTrue(file_exists($target . DS . 'rubbish2'));
unlink($target . DS . 'rubbish1');
unlink($target . DS . 'rubbish2');
}
private function execute($scriptPath, array $arguments)
{
$argv = array_merge([$scriptPath], $arguments);
ob_start();
include($scriptPath);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}