From 82dc5df47dd2a31a7b1e611abbd685acf996b17c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 3 Jan 2023 13:19:21 +0100 Subject: [PATCH] Extract public getTagsInGroup helper --- .../Publications/Models/PublicationTags.php | 8 ++++++- .../tests/Feature/PublicationTagsTest.php | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationTags.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationTags.php index 6cdb35fc269..2227062835d 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationTags.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationTags.php @@ -35,6 +35,12 @@ public function getTags(): Collection return $this->tags; } + /** @return array */ + public function getTagsInGroup(string $name): array + { + return $this->tags->get($name) ?? []; + } + /** * @param array|string $values * @return $this @@ -65,7 +71,7 @@ public function addTagGroups(array $tags): self */ public function addTagsToGroup(string $name, array|string $values): self { - $this->tags->put($name, array_merge($this->tags->get($name, []), (array) $values)); + $this->tags->put($name, array_merge($this->getTagsInGroup($name), (array) $values)); return $this; } diff --git a/packages/framework/tests/Feature/PublicationTagsTest.php b/packages/framework/tests/Feature/PublicationTagsTest.php index 8b4af9b0597..1f1ad3fe7ec 100644 --- a/packages/framework/tests/Feature/PublicationTagsTest.php +++ b/packages/framework/tests/Feature/PublicationTagsTest.php @@ -41,6 +41,30 @@ public function testGetTags() $this->assertEquals(new Collection(['foo' => ['bar', 'baz']]), (new PublicationTags())->getTags()); } + public function testGetTagsInGroup() + { + $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + + $this->assertEquals(['bar', 'baz'], (new PublicationTags())->getTagsInGroup('foo')); + } + + public function testGetTagsInGroupOnlyReturnTagsForTheSpecifiedGroup() + { + $this->file('tags.json', json_encode([ + 'foo' => ['bar', 'baz'], + 'bar' => ['foo', 'baz'], + ])); + + $this->assertEquals(['foo', 'baz'], (new PublicationTags())->getTagsInGroup('bar')); + } + + public function testGetTagsInGroupReturnsEmptyArrayWhenGroupDoesNotExist() + { + $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + + $this->assertEquals([], (new PublicationTags())->getTagsInGroup('bar')); + } + public function testCanAddTagGroup() { $tags = new PublicationTags();