Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Dec 21, 2023
1 parent e0d8752 commit f7181f2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 5 deletions.
9 changes: 8 additions & 1 deletion tests/Stub/TagFieldTestBlogTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\ORM\ManyManyList;

/**
* @property int Sort
* @method ManyManyList|TagFieldTestBlogPost[] BlogPosts()
*/
class TagFieldTestBlogTag extends DataObject implements TestOnly
Expand All @@ -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;
}
}
141 changes: 141 additions & 0 deletions tests/TagFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 6 additions & 4 deletions tests/TagFieldTest.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f7181f2

Please sign in to comment.