diff --git a/tests/Http/Controllers/Api/Projects/ProjectAnnotationLabelsTest.php b/tests/Http/Controllers/Api/Projects/ProjectAnnotationLabelsTest.php index b73c3c4..a37490e 100644 --- a/tests/Http/Controllers/Api/Projects/ProjectAnnotationLabelsTest.php +++ b/tests/Http/Controllers/Api/Projects/ProjectAnnotationLabelsTest.php @@ -102,7 +102,6 @@ public function testGetProjectAnnotationLabelsOnlyVideos() ]); } - public function testGetProjectAnnotationLabelsNoLabels() { $this->beEditor(); @@ -110,4 +109,52 @@ public function testGetProjectAnnotationLabelsNoLabels() ->assertStatus(200) ->assertJsonCount(0); } + + public function testGetProjectAnnotationLabelsSorting() + { + $id = $this->project()->id; + $img = ImageTest::create(['volume_id' => $this->volume()->id, 'filename' => 'abc.jpg']); + $a = ImageAnnotationTest::create(['image_id' => $img]); + $l1 = LabelTest::create(['name' => '1']); + $l3 = LabelTest::create(['name' => '11']); + ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l1->id]); + ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l3->id]); + + $videoVolume = VolumeTest::create(['media_type_id' => MediaType::video(), 'creator_id' => $this->volume()->creator_id]); + $this->project()->volumes()->attach($videoVolume->id); + $vid = VideoTest::create(['volume_id' => $videoVolume, 'filename' => 'abc.jpg']); + $a2 = VideoAnnotationTest::create(['video_id' => $vid]); + $l2 = LabelTest::create(['name' => '2']); + VideoAnnotationLabelTest::create(['annotation_id' => $a2->id, 'label_id' => $l2->id]); + + $this->beEditor(); + $this->getJson("/api/v1/projects/{$id}/label-count") + ->assertStatus(200) + ->assertJsonCount(3) + ->assertExactJson( + [ + [ + "id" => $l1->id, + "color" => $l1->color, + "name" => $l1->name, + "label_tree_id" => $l1->label_tree_id, + "count" => 1 + ], + [ + "id" => $l2->id, + "color" => $l2->color, + "name" => $l2->name, + "label_tree_id" => $l2->label_tree_id, + "count" => 1 + ], + [ + "id" => $l3->id, + "color" => $l3->color, + "name" => $l3->name, + "label_tree_id" => $l3->label_tree_id, + "count" => 1 + ] + ] + ); + } } diff --git a/tests/Http/Controllers/Api/Volumes/VolumeAnnotationLabelsTest.php b/tests/Http/Controllers/Api/Volumes/VolumeAnnotationLabelsTest.php index 6dbf3df..0e4c0c4 100644 --- a/tests/Http/Controllers/Api/Volumes/VolumeAnnotationLabelsTest.php +++ b/tests/Http/Controllers/Api/Volumes/VolumeAnnotationLabelsTest.php @@ -3,12 +3,14 @@ namespace Biigle\Tests\Modules\Largo\Http\Controllers\Api\Volumes; use ApiTestCase; +use Carbon\Carbon; use Biigle\MediaType; use Biigle\Tests\ImageTest; use Biigle\Tests\LabelTest; use Biigle\Tests\VideoTest; use Biigle\Tests\ImageAnnotationTest; use Biigle\Tests\VideoAnnotationTest; +use Biigle\Tests\AnnotationSessionTest; use Biigle\Tests\ImageAnnotationLabelTest; use Biigle\Tests\VideoAnnotationLabelTest; @@ -92,4 +94,208 @@ public function testGetImageVolumeAnnotationLabelsNoLabels() ->assertJsonCount(0); } + public function testGetVolumeAnnotationLabelsSorting() + { + $id = $this->volume()->id; + $img = ImageTest::create(['volume_id' => $id, 'filename' => 'abc.jpg']); + $a = ImageAnnotationTest::create(['image_id' => $img]); + $l1 = LabelTest::create(['name' => '1']); + $l2 = LabelTest::create(['name' => '11']); + $l3 = LabelTest::create(['name' => '2']); + ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l1->id]); + ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l2->id]); + ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l3->id]); + + $this->beEditor(); + $this->getJson("/api/v1/volume/{$id}/label-count") + ->assertStatus(200) + ->assertJsonCount(3) + ->assertExactJson( + [ + [ + "id" => $l1->id, + "color" => $l1->color, + "name" => $l1->name, + "label_tree_id" => $l1->label_tree_id, + "count" => 1 + ], + [ + "id" => $l3->id, + "color" => $l3->color, + "name" => $l3->name, + "label_tree_id" => $l3->label_tree_id, + "count" => 1 + ], + [ + "id" => $l2->id, + "color" => $l2->color, + "name" => $l2->name, + "label_tree_id" => $l2->label_tree_id, + "count" => 1 + ] + ] + ); + } + + public function testGetVolumeAnnotationLabelsAnnotationSession() + { + $id = $this->volume()->id; + $image = ImageTest::create(['volume_id' => $id, 'filename' => 'abc.jpg']); + + $l1 = LabelTest::create(['name' => '1']); + + $l2 = LabelTest::create(['name' => '2']); + + $l3 = LabelTest::create(['name' => '3']); + + $a1 = ImageAnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::yesterday(), + ]); + + $a2 = ImageAnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::today(), + ]); + + $a3 = ImageAnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::yesterday(), + ]); + + ImageAnnotationLabelTest::create([ + 'annotation_id' => $a1->id, + 'label_id' => $l1->id, + 'user_id' => $this->editor()->id, + ]); + + ImageAnnotationLabelTest::create([ + 'annotation_id' => $a2->id, + 'label_id' => $l2->id, + 'user_id' => $this->editor()->id, + ]); + + ImageAnnotationLabelTest::create([ + 'annotation_id' => $a3->id, + 'label_id' => $l3->id, + 'user_id' => $this->admin()->id, + ]); + + $this->beEditor(); + + // test hide own + $session = AnnotationSessionTest::create([ + 'volume_id' => $id, + 'starts_at' => Carbon::today(), + 'ends_at' => Carbon::tomorrow(), + 'hide_own_annotations' => true, + 'hide_other_users_annotations' => false, + ]); + + $session->users()->attach($this->editor()); + + $this->getJson("/api/v1/volume/{$id}/label-count") + ->assertStatus(200) + ->assertExactJson([ + [ + "id" => $l2->id, + "color" => $l2->color, + "name" => $l2->name, + "label_tree_id" => $l2->label_tree_id, + "count" => 1 + ], + [ + "id" => $l3->id, + "color" => $l3->color, + "name" => $l3->name, + "label_tree_id" => $l3->label_tree_id, + "count" => 1 + ], + ]); + + // test hide other + $session->hide_own_annotations = false; + $session->hide_other_users_annotations = true; + $session->save(); + + $this->getJson("/api/v1/volume/{$id}/label-count") + ->assertStatus(200) + ->assertExactJson([ + [ + "id" => $l1->id, + "color" => $l1->color, + "name" => $l1->name, + "label_tree_id" => $l1->label_tree_id, + "count" => 1 + ], + [ + "id" => $l2->id, + "color" => $l2->color, + "name" => $l2->name, + "label_tree_id" => $l2->label_tree_id, + "count" => 1 + ], + ]); + + // test hide both + $session->hide_own_annotations = true; + $session->save(); + + $this->getJson("/api/v1/volume/{$id}/label-count") + ->assertStatus(200) + ->assertExactJson([ + [ + "id" => $l2->id, + "color" => $l2->color, + "name" => $l2->name, + "label_tree_id" => $l2->label_tree_id, + "count" => 1 + ], + ]); + } + + public function testGetVolumeAnnotationLabelsAnnotationSessionEdgeCaseHideOther() + { + $id = $this->volume()->id; + $image = ImageTest::create(['volume_id' => $id]); + + $l1 = LabelTest::create(); + + $l2 = LabelTest::create(); + + $a1 = ImageAnnotationTest::create([ + 'image_id' => $image->id, + ]); + ImageAnnotationLabelTest::create([ + 'annotation_id' => $a1->id, + 'user_id' => $this->editor()->id, + 'label_id' => $l1->id, + ]); + ImageAnnotationLabelTest::create([ + 'annotation_id' => $a1->id, + 'user_id' => $this->admin()->id, + 'label_id' => $l2->id, + ]); + $session = AnnotationSessionTest::create([ + 'volume_id' => $id, + 'starts_at' => Carbon::yesterday(), + 'ends_at' => Carbon::tomorrow(), + 'hide_own_annotations' => false, + 'hide_other_users_annotations' => true, + ]); + $session->users()->attach($this->editor()); + + $this->beEditor(); + $this->getJson("/api/v1/volume/{$id}/label-count") + ->assertStatus(200) + ->assertExactJson([ + [ + "id" => $l1->id, + "color" => $l1->color, + "name" => $l1->name, + "label_tree_id" => $l1->label_tree_id, + "count" => 1 + ], + ]); + } }