From 69112fdee34bde33a84bc5666d79a956e047bc1d Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 24 Oct 2020 18:55:07 +0100 Subject: [PATCH] phpunit9 --- ext/index/test.php | 15 +++++++++++++++ ext/pools/main.php | 23 +++++++++++++++-------- ext/pools/test.php | 36 +++++++++++++++++++++++++----------- ext/relationships/test.php | 12 ++++++------ tests/bootstrap.php | 21 ++++++++------------- tests/phpunit.xml | 33 +++++++++++++++++---------------- 6 files changed, 86 insertions(+), 54 deletions(-) diff --git a/ext/index/test.php b/ext/index/test.php index 74d786f4..5ffbfa9e 100644 --- a/ext/index/test.php +++ b/ext/index/test.php @@ -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]]); diff --git a/ext/pools/main.php b/ext/pools/main.php index 2ef8f1ae..edee61eb 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -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]); } } diff --git a/ext/pools/test.php b/ext/pools/test.php index c6e91432..5e574f27 100644 --- a/ext/pools/test.php +++ b/ext/pools/test.php @@ -1,8 +1,22 @@ 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() diff --git a/ext/relationships/test.php b/ext/relationships/test.php index b5667073..8b5eaf5a 100644 --- a/ext/relationships/test.php +++ b/ext/relationships/test.php @@ -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 diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3bb01e3c..51d3ae6c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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) diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 9b86d9f4..2ded9aa8 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,17 +1,18 @@ - - - - ../core/ - - - ../ext/ - - - - - ../core - ../ext - ../themes/default - - + + + + + ../core + ../ext + ../themes/default + + + + + ../core/ + + + ../ext/ + +