2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-01-28 21:19:59 +00:00
|
|
|
class RelationshipsTest extends ShimmiePHPUnitTestCase
|
2019-07-05 15:15:38 +00:00
|
|
|
{
|
2020-01-29 20:22:50 +00:00
|
|
|
//=================================================================
|
|
|
|
// Set by box
|
|
|
|
//=================================================================
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testNoParent(): array
|
2019-07-05 15:15:38 +00:00
|
|
|
{
|
|
|
|
$this->log_in_as_user();
|
|
|
|
|
|
|
|
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
|
|
|
$image_id_2 = $this->post_image("tests/bedroom_workshop.jpg", "pbx");
|
|
|
|
$image_id_3 = $this->post_image("tests/favicon.png", "pbx");
|
|
|
|
|
|
|
|
$image_1 = Image::by_id($image_id_1);
|
|
|
|
$image_2 = Image::by_id($image_id_2);
|
|
|
|
$image_3 = Image::by_id($image_id_3);
|
|
|
|
|
|
|
|
$this->assertNull($image_1->parent_id);
|
|
|
|
$this->assertNull($image_2->parent_id);
|
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertFalse($image_1->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testNoParent
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testSetParent($imgs): array
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testNoParent();
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new ImageRelationshipSetEvent($image_2->id, $image_1->id));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_1 = Image::by_id($image_1->id);
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
|
|
|
$image_3 = Image::by_id($image_3->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
|
|
|
$this->assertNull($image_1->parent_id);
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assertEquals($image_1->id, $image_2->parent_id);
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertTrue($image_1->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testSetParent
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testChangeParent($imgs): array
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testSetParent(null);
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new ImageRelationshipSetEvent($image_2->id, $image_3->id));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_1 = Image::by_id($image_1->id);
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
|
|
|
$image_3 = Image::by_id($image_3->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
|
|
|
$this->assertNull($image_1->parent_id);
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assertEquals($image_3->id, $image_2->parent_id);
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertTrue($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-10-24 21:48:53 +00:00
|
|
|
/**
|
|
|
|
* @depends testSetParent
|
|
|
|
*/
|
|
|
|
public function testSearch($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $this->testSetParent(null);
|
|
|
|
|
|
|
|
$this->assert_search_results(["parent:any"], [$image_2->id]);
|
|
|
|
$this->assert_search_results(["parent:none"], [$image_3->id, $image_1->id]);
|
|
|
|
$this->assert_search_results(["parent:{$image_1->id}"], [$image_2->id]);
|
|
|
|
|
|
|
|
$this->assert_search_results(["child:any"], [$image_1->id]);
|
|
|
|
$this->assert_search_results(["child:none"], [$image_3->id, $image_2->id]);
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testChangeParent
|
|
|
|
*/
|
|
|
|
public function testRemoveParent($imgs)
|
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testChangeParent(null);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
global $database;
|
2020-01-30 09:01:19 +00:00
|
|
|
$database->execute(
|
|
|
|
"UPDATE images SET parent_id=NULL, has_children=:false",
|
2020-10-26 19:38:51 +00:00
|
|
|
["false"=>false]
|
2020-01-30 09:01:19 +00:00
|
|
|
);
|
2020-01-29 20:22:50 +00:00
|
|
|
// FIXME: send_event(new ImageRelationshipSetEvent($image_2->id, null));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_1 = Image::by_id($image_1->id);
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
|
|
|
$image_3 = Image::by_id($image_3->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
|
|
|
$this->assertNull($image_1->parent_id);
|
|
|
|
$this->assertNull($image_2->parent_id);
|
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_3->has_children);
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
//=================================================================
|
|
|
|
// Set by tag
|
|
|
|
//=================================================================
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testSetParentByTagBase(): array
|
2019-07-05 15:15:38 +00:00
|
|
|
{
|
|
|
|
$this->log_in_as_user();
|
|
|
|
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
|
|
|
|
$image_id_2 = $this->post_image("tests/bedroom_workshop.jpg", "pbx");
|
|
|
|
$image_id_3 = $this->post_image("tests/favicon.png", "pbx");
|
|
|
|
|
|
|
|
$image_1 = Image::by_id($image_id_1);
|
|
|
|
$image_2 = Image::by_id($image_id_2);
|
|
|
|
$image_3 = Image::by_id($image_id_3);
|
|
|
|
|
|
|
|
$this->assertNull($image_1->parent_id);
|
|
|
|
$this->assertNull($image_2->parent_id);
|
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertFalse($image_1->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testSetParentByTagBase
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testSetParentByTag($imgs): array
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testSetParentByTagBase();
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new TagSetEvent($image_2, ["pbx", "parent:{$image_1->id}"]));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_1 = Image::by_id($image_1->id);
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
|
|
|
$image_3 = Image::by_id($image_3->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assertEquals(["pbx"], $image_2->get_tag_array());
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->assertNull($image_1->parent_id);
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assertEquals($image_1->id, $image_2->parent_id);
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertTrue($image_1->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertFalse($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testSetParentByTag
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public function testSetChildByTag($imgs): array
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testSetParentByTag(null);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new TagSetEvent($image_3, ["pbx", "child:{$image_1->id}"]));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_1 = Image::by_id($image_1->id);
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
|
|
|
$image_3 = Image::by_id($image_3->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->assertEquals(["pbx"], $image_3->get_tag_array());
|
|
|
|
$this->assertEquals($image_3->id, $image_1->parent_id);
|
|
|
|
$this->assertEquals($image_1->id, $image_2->parent_id);
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->assertNull($image_3->parent_id);
|
|
|
|
$this->assertTrue($image_1->has_children);
|
|
|
|
$this->assertFalse($image_2->has_children);
|
|
|
|
$this->assertTrue($image_3->has_children);
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
return [$image_1, $image_2, $image_3];
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testSetChildByTag
|
|
|
|
*/
|
|
|
|
public function testRemoveParentByTag($imgs)
|
|
|
|
{
|
2020-10-24 17:55:07 +00:00
|
|
|
[$image_1, $image_2, $image_3] = $this->testSetChildByTag(null);
|
2020-03-13 09:23:54 +00:00
|
|
|
assert(!is_null($image_3));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// check parent is set
|
|
|
|
$this->assertEquals($image_2->parent_id, $image_1->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// un-set it
|
|
|
|
send_event(new TagSetEvent($image_2, ["pbx", "parent:none"]));
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// refresh data from database
|
|
|
|
$image_2 = Image::by_id($image_2->id);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
// check it was unset
|
|
|
|
$this->assertEquals(["pbx"], $image_2->get_tag_array());
|
|
|
|
$this->assertNull($image_2->parent_id);
|
2019-07-05 15:15:38 +00:00
|
|
|
}
|
|
|
|
}
|