2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-01-28 21:19:59 +00:00
|
|
|
class RatingsTest extends ShimmiePHPUnitTestCase
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRatingSafe(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->log_in_as_user();
|
|
|
|
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image = Image::by_id_ex($image_id);
|
2020-01-29 01:47:43 +00:00
|
|
|
send_event(new RatingSetEvent($image, "s"));
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
# search for it in various ways
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assert_search_results(["rating=Safe"], [$image_id]);
|
|
|
|
$this->assert_search_results(["rating=s"], [$image_id]);
|
|
|
|
$this->assert_search_results(["rating=sqe"], [$image_id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
# test that search by tag still works
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assert_search_results(["pbx"], [$image_id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
# searching for a different rating should return nothing
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assert_search_results(["rating=q"], []);
|
2020-01-29 01:47:43 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRatingExplicit(): void
|
2020-01-29 01:47:43 +00:00
|
|
|
{
|
2020-01-29 11:30:52 +00:00
|
|
|
global $config;
|
|
|
|
$config->set_array("ext_rating_anonymous_privs", ["s", "q"]);
|
2020-01-29 01:47:43 +00:00
|
|
|
$this->log_in_as_user();
|
|
|
|
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image = Image::by_id_ex($image_id);
|
2020-01-29 01:47:43 +00:00
|
|
|
send_event(new RatingSetEvent($image, "e"));
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
# the explicit image shouldn't show up in anon's searches
|
2020-01-29 01:47:43 +00:00
|
|
|
$this->log_out();
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assert_search_results(["pbx"], []);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-12-31 18:48:49 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testUserConfig(): void
|
2023-12-31 18:48:49 +00:00
|
|
|
{
|
|
|
|
global $config, $user_config;
|
|
|
|
|
|
|
|
// post a safe image and an explicit image
|
|
|
|
$this->log_in_as_user();
|
|
|
|
$image_id_e = $this->post_image("tests/bedroom_workshop.jpg", "pbx");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image_e = Image::by_id_ex($image_id_e);
|
2023-12-31 18:48:49 +00:00
|
|
|
send_event(new RatingSetEvent($image_e, "e"));
|
|
|
|
$image_id_s = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image_s = Image::by_id_ex($image_id_s);
|
2023-12-31 18:48:49 +00:00
|
|
|
send_event(new RatingSetEvent($image_s, "s"));
|
|
|
|
|
|
|
|
// user is allowed to see all
|
|
|
|
$config->set_array("ext_rating_user_privs", ["s", "q", "e"]);
|
|
|
|
|
|
|
|
// user prefers safe-only by default
|
|
|
|
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
|
|
|
|
|
|
|
|
// search with no tags should return only safe image
|
|
|
|
$this->assert_search_results([], [$image_id_s]);
|
|
|
|
|
|
|
|
// specifying a rating should return only that rating
|
|
|
|
$this->assert_search_results(["rating=e"], [$image_id_e]);
|
|
|
|
$this->assert_search_results(["rating=s"], [$image_id_s]);
|
|
|
|
|
|
|
|
// If user prefers to see all images, going to the safe image
|
|
|
|
// and clicking next should show the explicit image
|
|
|
|
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s", "q", "e"]);
|
2024-08-31 20:22:54 +00:00
|
|
|
$next = $image_s->get_next();
|
|
|
|
$this->assertNotNull($next);
|
|
|
|
$this->assertEquals($next->id, $image_id_e);
|
2023-12-31 18:48:49 +00:00
|
|
|
|
|
|
|
// If the user prefers to see only safe images by default, then
|
|
|
|
// going to the safe image and clicking next should not show
|
|
|
|
// the explicit image (See bug #984)
|
|
|
|
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
|
|
|
|
$this->assertEquals($image_s->get_next(), null);
|
|
|
|
}
|
|
|
|
|
2024-01-07 04:17:22 +00:00
|
|
|
public function testCountImages(): void
|
|
|
|
{
|
|
|
|
global $config, $user_config;
|
|
|
|
|
|
|
|
$this->log_in_as_user();
|
|
|
|
|
|
|
|
$image_id_s = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image_s = Image::by_id_ex($image_id_s);
|
2024-01-07 04:17:22 +00:00
|
|
|
send_event(new RatingSetEvent($image_s, "s"));
|
|
|
|
$image_id_q = $this->post_image("tests/favicon.png", "favicon");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image_q = Image::by_id_ex($image_id_q);
|
2024-01-07 04:17:22 +00:00
|
|
|
send_event(new RatingSetEvent($image_q, "q"));
|
|
|
|
$image_id_e = $this->post_image("tests/bedroom_workshop.jpg", "bedroom");
|
2024-02-20 00:22:25 +00:00
|
|
|
$image_e = Image::by_id_ex($image_id_e);
|
2024-01-07 04:17:22 +00:00
|
|
|
send_event(new RatingSetEvent($image_e, "e"));
|
|
|
|
|
|
|
|
$config->set_array("ext_rating_user_privs", ["s", "q"]);
|
|
|
|
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
|
|
|
|
|
|
|
|
$this->assertEquals(1, Search::count_images(["rating=s"]), "UserClass has access to safe, show safe");
|
|
|
|
$this->assertEquals(2, Search::count_images(["rating=*"]), "UserClass has access to s/q - if user asks for everything, show those two but hide e");
|
|
|
|
$this->assertEquals(1, Search::count_images(), "If search doesn't specify anything, check the user defaults");
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:48:49 +00:00
|
|
|
// reset the user config to defaults at the end of every test so
|
|
|
|
// that it doesn't mess with other unrelated tests
|
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
global $config, $user_config;
|
|
|
|
$config->set_array("ext_rating_user_privs", ["?", "s", "q", "e"]);
|
|
|
|
|
|
|
|
$this->log_in_as_user();
|
|
|
|
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["?", "s", "q", "e"]);
|
2024-01-09 22:47:10 +00:00
|
|
|
|
|
|
|
parent::tearDown();
|
2023-12-31 18:48:49 +00:00
|
|
|
}
|
2009-08-13 20:42:48 +00:00
|
|
|
}
|