From f7181f2ff8c7b181b34dba243dcf31c51aa668d4 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 22 Dec 2023 10:39:25 +1300 Subject: [PATCH] Fixes. --- tests/Stub/TagFieldTestBlogTag.php | 9 +- tests/TagFieldTest.php | 141 +++++++++++++++++++++++++++++ tests/TagFieldTest.yml | 10 +- 3 files changed, 155 insertions(+), 5 deletions(-) diff --git a/tests/Stub/TagFieldTestBlogTag.php b/tests/Stub/TagFieldTestBlogTag.php index 9a8918e..f35a285 100644 --- a/tests/Stub/TagFieldTestBlogTag.php +++ b/tests/Stub/TagFieldTestBlogTag.php @@ -7,6 +7,7 @@ use SilverStripe\ORM\ManyManyList; /** + * @property int Sort * @method ManyManyList|TagFieldTestBlogPost[] BlogPosts() */ class TagFieldTestBlogTag extends DataObject implements TestOnly @@ -16,10 +17,16 @@ class TagFieldTestBlogTag extends DataObject implements TestOnly private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC'; private static $db = [ - 'Title' => 'Varchar(200)' + 'Title' => 'Varchar(200)', + 'Sort' => 'Int', ]; private static $belongs_many_many = [ 'BlogPosts' => TagFieldTestBlogPost::class ]; + + public function getLabel(): string + { + return 'Label: ' . $this->Title; + } } diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index 356f405..bc020a7 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -2,10 +2,12 @@ namespace SilverStripe\TagField\Tests; +use ReflectionMethod; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; +use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\TagField\ReadonlyTagField; @@ -527,6 +529,145 @@ public function testItDisplaysWithSelectedValuesFromDataList() ); } + /** + * @dataProvider optionCasesProvider + */ + public function testGetOptionsWithConfigurableFields(?string $titleField, ?string $valueField, array $expected): void + { + $source = TagFieldTestBlogTag::get(); + + $field = new TagField('TestField', null, $source); + + if ($titleField) { + $field->setTitleField($titleField); + } + + if ($valueField) { + $field->setValueField($valueField); + } + + /** @see TagField::getOptions() */ + $getOptionsMethod = new ReflectionMethod($field, 'getOptions'); + + /** @var ArrayList $result */ + $result = $getOptionsMethod->invoke($field); + + $data = $result + ->map('Title', 'Value') + ->toArray(); + $this->assertSame($expected, $data, 'We expect specific fields to be present'); + } + + public function optionCasesProvider(): array + { + return [ + 'default fields' => [ + null, + null, + [ + 'Tag1' => 'Tag1', + '222' => '222' + ], + ], + 'Label > Sort' => [ + 'Label', + 'Sort', + [ + 'Label: Tag1' => 2, + 'Label: 222' => 1 + ], + ], + 'Sort > Title' => [ + 'Sort', + 'Title', + [ + 2 => 'Tag1', + 1 => '222' + ], + ], + ]; + } + + /** + * @dataProvider getTagsCasesProvider + */ + public function testGetTagsWithConfigurableFields( + ?string $titleField, + ?string $valueField, + ?string $searchField, + ?string $sortField, + string $searchSubject, + array $expected + ): void { + $tag3 = TagFieldTestBlogTag::create(); + $tag3->Title = 'Tag3'; + $tag3->Sort = 3; + $tag3->write(); + + $source = TagFieldTestBlogTag::get(); + + $field = new TagField('TestField', null, $source); + + if ($titleField) { + $field->setTitleField($titleField); + } + + if ($valueField) { + $field->setValueField($valueField); + } + + if ($searchField) { + $field->setSearchField($searchField); + } + + if ($sortField) { + $field->setSortField($sortField); + } + + /** @see TagField::getTags() */ + $getTagsMethod = new ReflectionMethod($field, 'getTags'); + + /** @var ArrayList $result */ + $result = $getTagsMethod->invoke($field, $searchSubject); + $data = []; + + foreach ($result as $item) { + $title = $item['Title']; + $value = $item['Value']; + $data[$title] = $value; + } + + $this->assertSame($expected, $data, 'We expect specific fields to be present'); + } + + public function getTagsCasesProvider(): array + { + return [ + 'default fields' => [ + null, + null, + null, + null, + 'Tag', + [ + 'Tag1' => 'Tag1', + 'Tag3' => 'Tag3', + ], + ], + 'custom fields' => [ + 'Label', + 'Sort', + 'Title', + 'Sort', + 'Tag', + [ + 'Label: Tag1' => 2, + 'Label: Tag3' => 3, + ], + ], + ]; + } + public function testGetSchemaDataDefaults() { $form = new Form(null, 'Form', new FieldList(), new FieldList()); diff --git a/tests/TagFieldTest.yml b/tests/TagFieldTest.yml index 750e0a0..a39cc1d 100755 --- a/tests/TagFieldTest.yml +++ b/tests/TagFieldTest.yml @@ -1,11 +1,13 @@ SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag: Tag1: - Title: Tag1 + Title: 'Tag1' + Sort: 2 Tag2: - Title: 222 + Title: '222' + Sort: 1 SilverStripe\TagField\Tests\Stub\TagFieldTestBlogPost: BlogPost1: - Title: BlogPost1 + Title: 'BlogPost1' BlogPost2: - Title: BlogPost2 + Title: 'BlogPost2' Tags: =>SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag.Tag1,=>SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag.Tag2