This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/relationships/test.php

226 lines
7.7 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2023-12-14 00:14:24 +00:00
use PHPUnit\Framework\Attributes\Depends;
class RelationshipsTest extends ShimmiePHPUnitTestCase
{
2020-01-29 20:22:50 +00:00
//=================================================================
// Set by box
//=================================================================
/**
* @return array{0: Image, 1: Image, 2: Image}
*/
public function testNoParent(): array
{
$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];
}
/**
* @return array{0:Image, 1:Image, 2:Image}
*/
2023-12-14 00:14:24 +00:00
#[Depends('testNoParent')]
public function testSetParent(): 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();
2020-01-29 20:22:50 +00:00
send_event(new ImageRelationshipSetEvent($image_2->id, $image_1->id));
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);
$this->assertNull($image_1['parent_id']);
$this->assertEquals($image_1->id, $image_2['parent_id']);
$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];
}
/**
* @return array{0:Image, 1:Image, 2:Image}
*/
2023-12-14 00:14:24 +00:00
#[Depends('testSetParent')]
public function testChangeParent(): array
2020-01-29 20:22:50 +00:00
{
[$image_1, $image_2, $image_3] = $this->testSetParent();
2020-01-29 20:22:50 +00:00
send_event(new ImageRelationshipSetEvent($image_2->id, $image_3->id));
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);
$this->assertNull($image_1['parent_id']);
$this->assertEquals($image_3->id, $image_2['parent_id']);
$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];
}
2023-12-14 00:14:24 +00:00
#[Depends('testSetParent')]
public function testSearch(): void
{
[$image_1, $image_2, $image_3] = $this->testSetParent();
$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]);
}
2023-12-14 00:14:24 +00:00
#[Depends('testChangeParent')]
public function testRemoveParent(): void
2020-01-29 20:22:50 +00:00
{
[$image_1, $image_2, $image_3] = $this->testChangeParent();
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",
2023-11-11 21:49:12 +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));
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);
$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
//=================================================================
/**
* @return array{0:Image, 1:Image, 2:Image}
*/
public function testSetParentByTagBase(): array
{
$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];
}
/**
* @return array{0:Image, 1:Image, 2:Image}
*/
2023-12-14 00:14:24 +00:00
#[Depends('testSetParentByTagBase')]
public function testSetParentByTag(): 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();
2020-01-29 20:22:50 +00:00
send_event(new TagSetEvent($image_2, ["pbx", "parent:{$image_1->id}"]));
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);
2020-01-29 20:22:50 +00:00
$this->assertEquals(["pbx"], $image_2->get_tag_array());
$this->assertNull($image_1['parent_id']);
$this->assertEquals($image_1->id, $image_2['parent_id']);
$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];
}
/**
* @return array{0:Image, 1:Image, 2:Image}
*/
2023-12-14 00:14:24 +00:00
#[Depends('testSetParentByTag')]
public function testSetChildByTag(): array
2020-01-29 20:22:50 +00:00
{
[$image_1, $image_2, $image_3] = $this->testSetParentByTag();
2020-01-29 20:22:50 +00:00
send_event(new TagSetEvent($image_3, ["pbx", "child:{$image_1->id}"]));
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);
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']);
$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];
}
2023-12-14 00:14:24 +00:00
#[Depends('testSetChildByTag')]
public function testRemoveParentByTag(): void
2020-01-29 20:22:50 +00:00
{
[$image_1, $image_2, $image_3] = $this->testSetChildByTag();
2020-01-29 20:22:50 +00:00
// check parent is set
$this->assertEquals($image_2['parent_id'], $image_1->id);
2020-01-29 20:22:50 +00:00
// un-set it
send_event(new TagSetEvent($image_2, ["pbx", "parent:none"]));
2020-01-29 20:22:50 +00:00
// refresh data from database
$image_2 = Image::by_id($image_2->id);
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']);
}
}