This commit is contained in:
Shish 2020-10-24 18:55:07 +01:00
parent 4bf2cb8ee7
commit 69112fdee3
6 changed files with 86 additions and 54 deletions

View File

@ -77,18 +77,21 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testTagSearchNoResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["maumaumau"], []);
}
/** @depends testUpload */
public function testTagSearchOneResult($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["pbx"], [$image_ids[0]]);
}
/** @depends testUpload */
public function testTagSearchManyResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["computer"], [$image_ids[1], $image_ids[0]]);
}
@ -98,6 +101,7 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testMultiTagSearchNoResults($image_ids)
{
$image_ids = $this->testUpload();
# multiple tags, one of which doesn't exist
# (test the "one tag doesn't exist = no hits" path)
$this->assert_search_results(["computer", "asdfasdfwaffle"], []);
@ -106,12 +110,14 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testMultiTagSearchOneResult($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["computer", "screenshot"], [$image_ids[0]]);
}
/** @depends testUpload */
public function testMultiTagSearchManyResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["computer", "thing"], [$image_ids[1], $image_ids[0]]);
}
@ -121,12 +127,14 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testMetaSearchNoResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["hash=1234567890"], []);
}
/** @depends testUpload */
public function testMetaSearchOneResult($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["hash=feb01bab5698a11dd87416724c7a89e3"], [$image_ids[0]]);
$this->assert_search_results(["md5=feb01bab5698a11dd87416724c7a89e3"], [$image_ids[0]]);
$this->assert_search_results(["id={$image_ids[1]}"], [$image_ids[1]]);
@ -136,6 +144,7 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testMetaSearchManyResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["size=640x480"], [$image_ids[1], $image_ids[0]]);
$this->assert_search_results(["tags=5"], [$image_ids[1], $image_ids[0]]);
$this->assert_search_results(["ext=jpg"], [$image_ids[1], $image_ids[0]]);
@ -147,12 +156,14 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testWildSearchNoResults($image_ids)
{
$image_ids = $this->testUpload();
$this->assert_search_results(["asdfasdf*"], []);
}
/** @depends testUpload */
public function testWildSearchOneResult($image_ids)
{
$image_ids = $this->testUpload();
// Only the first image matches both the wildcard and the tag.
// This checks for https://github.com/shish/shimmie2/issues/547
$this->assert_search_results(["comp*", "screenshot"], [$image_ids[0]]);
@ -161,6 +172,7 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testWildSearchManyResults($image_ids)
{
$image_ids = $this->testUpload();
// two images match comp* - one matches it once,
// one matches it twice
$this->assert_search_results(["comp*"], [$image_ids[1], $image_ids[0]]);
@ -172,6 +184,7 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testMixedSearchTagMeta($image_ids)
{
$image_ids = $this->testUpload();
// multiple tags, many results
$this->assert_search_results(["computer", "size=640x480"], [$image_ids[1], $image_ids[0]]);
}
@ -184,6 +197,8 @@ class IndexTest extends ShimmiePHPUnitTestCase
/** @depends testUpload */
public function testNegative($image_ids)
{
$image_ids = $this->testUpload();
// negative tag, should have one result
$this->assert_search_results(["computer", "-pbx"], [$image_ids[1]]);

View File

@ -58,6 +58,16 @@ class PoolCreationEvent extends Event
}
}
class PoolDeletionEvent extends Event {
public $pool_id;
public function __construct(int $pool_id)
{
parent::__construct();
$this->pool_id = $pool_id;
}
}
class Pool
{
public $id;
@ -379,7 +389,7 @@ class Pools extends Extension
// Completely remove the given pool.
// -> Only admins and owners may do this
if ($user->can(Permissions::POOLS_ADMIN) || $user->id == $pool->user_id) {
$this->nuke_pool($pool_id);
send_event(new PoolDeletionEvent($pool_id));
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("pool/list"));
} else {
@ -797,19 +807,16 @@ class Pools extends Extension
/**
* HERE WE NUKE ENTIRE POOL. WE REMOVE POOLS AND POSTS FROM REMOVED POOL AND HISTORIES ENTRIES FROM REMOVED POOL.
*/
private function nuke_pool(int $poolID)
public function onPoolDeletion(PoolDeletionEvent $event)
{
global $user, $database;
$poolID = $event->pool_id;
$p_id = (int)$database->get_one("SELECT user_id FROM pools WHERE id = :pid", ["pid" => $poolID]);
if ($user->can(Permissions::POOLS_ADMIN)) {
$owner_id = (int)$database->get_one("SELECT user_id FROM pools WHERE id = :pid", ["pid" => $poolID]);
if ($owner_id == $user->id || $user->can(Permissions::POOLS_ADMIN)) {
$database->execute("DELETE FROM pool_history WHERE pool_id = :pid", ["pid" => $poolID]);
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid", ["pid" => $poolID]);
$database->execute("DELETE FROM pools WHERE id = :pid", ["pid" => $poolID]);
} elseif ($user->id == $p_id) {
$database->execute("DELETE FROM pool_history WHERE pool_id = :pid", ["pid" => $poolID]);
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid", ["pid" => $poolID]);
$database->execute("DELETE FROM pools WHERE id = :pid AND user_id = :uid", ["pid" => $poolID, "uid" => $user->id]);
}
}

View File

@ -1,8 +1,22 @@
<?php declare(strict_types=1);
class PoolsTest extends ShimmiePHPUnitTestCase
{
public function setUp(): void
{
parent::setUp();
// Clean up any leftovers to create a fresh test env
$this->log_in_as_admin();
global $database;
foreach ($database->get_col("SELECT id FROM pools") as $pool_id) {
send_event(new PoolDeletionEvent((int)$pool_id));
}
}
public function testAnon()
{
$this->log_out();
$this->get_page('pool/list');
$this->assert_title("Pools");
@ -34,7 +48,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testOnViewImage($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
global $config;
$config->set_bool(PoolsConfig::ADDER_ON_VIEW_IMAGE, true);
@ -48,7 +62,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testSearch($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$this->get_page("post/list/pool=$pool_id/1");
$this->assert_text("Pool");
@ -60,7 +74,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testList($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$this->get_page("pool/list");
$this->assert_text("Pool");
@ -69,7 +83,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testView($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$this->get_page("pool/view/$pool_id");
$this->assert_text("Pool");
@ -78,7 +92,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testHistory($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$this->get_page("pool/updated/$pool_id");
$this->assert_text("Pool");
@ -87,7 +101,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testImport($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$this->post_page("pool/import", [
"pool_id" => $pool_id,
@ -99,7 +113,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testRemovePosts($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$page = $this->post_page("pool/remove_posts", [
"pool_id" => $pool_id,
@ -107,13 +121,13 @@ class PoolsTest extends ShimmiePHPUnitTestCase
]);
$this->assertEquals("redirect", $page->mode);
return $args;
return [$pool_id, $image_ids];
}
/** @depends testRemovePosts */
public function testAddPosts($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testRemovePosts(null);
$page = $this->post_page("pool/add_posts", [
"pool_id" => $pool_id,
@ -125,7 +139,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
/** @depends testCreate */
public function testEditDescription($args)
{
[$pool_id, $image_ids] = $args;
[$pool_id, $image_ids] = $this->testCreate();
$page = $this->post_page("pool/edit_description", [
"pool_id" => $pool_id,
@ -133,7 +147,7 @@ class PoolsTest extends ShimmiePHPUnitTestCase
]);
$this->assertEquals("redirect", $page->mode);
return $args;
return [$pool_id, $image_ids];
}
public function testNuke()

View File

@ -32,7 +32,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testSetParent($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testNoParent();
send_event(new ImageRelationshipSetEvent($image_2->id, $image_1->id));
@ -56,7 +56,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testChangeParent($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testSetParent(null);
send_event(new ImageRelationshipSetEvent($image_2->id, $image_3->id));
// refresh data from database
@ -79,7 +79,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testRemoveParent($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testChangeParent(null);
global $database;
$database->execute(
@ -131,7 +131,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testSetParentByTag($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testSetParentByTagBase();
send_event(new TagSetEvent($image_2, ["pbx", "parent:{$image_1->id}"]));
@ -156,7 +156,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testSetChildByTag($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testSetParentByTag(null);
send_event(new TagSetEvent($image_3, ["pbx", "child:{$image_1->id}"]));
@ -181,7 +181,7 @@ class RelationshipsTest extends ShimmiePHPUnitTestCase
*/
public function testRemoveParentByTag($imgs)
{
[$image_1, $image_2, $image_3] = $imgs;
[$image_1, $image_2, $image_3] = $this->testSetChildByTag(null);
assert(!is_null($image_3));
// check parent is set

View File

@ -66,26 +66,21 @@ abstract class ShimmiePHPUnitTestCase extends TestCase
$this->markTestSkipped("$class not supported with this database");
}
// If we have a parent test, don't wipe out the state they gave us
if (!$this->getDependencyInput()) {
// things to do after bootstrap and before request
// log in as anon
self::log_out();
foreach ($database->get_col("SELECT id FROM images") as $image_id) {
send_event(new ImageDeletionEvent(Image::by_id((int)$image_id), true));
}
// Set up a clean environment for each test
self::log_out();
foreach ($database->get_col("SELECT id FROM images") as $image_id) {
send_event(new ImageDeletionEvent(Image::by_id((int)$image_id), true));
}
$_tracer->end();
$_tracer->end(); # setUp
$_tracer->begin("test");
}
public function tearDown(): void
{
global $_tracer;
$_tracer->end();
$_tracer->end();
$_tracer->end(); # test
$_tracer->end(); # $this->getName()
$_tracer->clear();
$_tracer->flush("tests/trace.json");
}
@ -94,7 +89,7 @@ abstract class ShimmiePHPUnitTestCase extends TestCase
{
parent::tearDownAfterClass();
global $_tracer;
$_tracer->end();
$_tracer->end(); # get_called_class()
}
protected static function create_user(string $name)

View File

@ -1,17 +1,18 @@
<phpunit bootstrap="../tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="core">
<directory suffix="test.php">../core/</directory>
</testsuite>
<testsuite name="ext">
<directory suffix="test.php">../ext/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../core</directory>
<directory suffix=".php">../ext</directory>
<directory suffix=".php">../themes/default</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">../core</directory>
<directory suffix=".php">../ext</directory>
<directory suffix=".php">../themes/default</directory>
</include>
</coverage>
<testsuites>
<testsuite name="core">
<directory suffix="test.php">../core/</directory>
</testsuite>
<testsuite name="ext">
<directory suffix="test.php">../ext/</directory>
</testsuite>
</testsuites>
</phpunit>