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
|
|
|
|
//=================================================================
|
|
|
|
|
|
|
|
public function testNoParent()
|
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
|
|
|
|
*/
|
|
|
|
public function testSetParent($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
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
|
|
|
|
*/
|
|
|
|
public function testChangeParent($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
|
|
|
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-01-29 20:22:50 +00:00
|
|
|
/**
|
|
|
|
* @depends testChangeParent
|
|
|
|
*/
|
|
|
|
public function testRemoveParent($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
global $database;
|
2020-01-29 20:36:25 +00:00
|
|
|
$database->execute("UPDATE images SET parent_id=NULL, has_children=:false", ["false"=>false]);
|
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
|
|
|
|
//=================================================================
|
|
|
|
|
|
|
|
public function testSetParentByTagBase()
|
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
|
|
|
|
*/
|
|
|
|
public function testSetParentByTag($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
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
|
|
|
|
*/
|
|
|
|
public function testSetChildByTag($imgs)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
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)
|
|
|
|
{
|
|
|
|
[$image_1, $image_2, $image_3] = $imgs;
|
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
|
|
|
}
|
|
|
|
}
|