Skip to content

Commit

Permalink
Add tests for schema generation, further switch to short array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Nov 16, 2018
1 parent 3de6d94 commit 8030ddf
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 31 deletions.
48 changes: 43 additions & 5 deletions tests/StringTagFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace SilverStripe\TagField\Tests;

use PHPUnit_Framework_TestCase;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\TagField\StringTagField;
use SilverStripe\TagField\Tests\Stub\StringTagFieldTestBlogPost;

Expand All @@ -20,16 +23,16 @@ class StringTagFieldTest extends SapphireTest
/**
* @var array
*/
protected static $extra_dataobjects = array(
protected static $extra_dataobjects = [
StringTagFieldTestBlogPost::class,
);
];

public function testItSavesTagsOnNewRecords()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');

$field = new StringTagField('Tags');
$field->setValue(array('Tag1', 'Tag2'));
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);

$record->write();
Expand All @@ -56,7 +59,7 @@ public function testItSavesTagsOnExistingRecords()
$record->write();

$field = new StringTagField('Tags');
$field->setValue(array('Tag1', 'Tag2'));
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);

$this->assertEquals('Tag1,Tag2', $record->Tags);
Expand Down Expand Up @@ -104,9 +107,44 @@ public function testItSuggestsTags()
);
}

public function testGetSchemaDataDefaults()
{
$form = new Form(null, 'Form', new FieldList(), new FieldList());
$field = new StringTagField('TestField', 'Test Field', ['one', 'two']);
$field->setForm($form);

$field
->setShouldLazyLoad(false)
->setCanCreate(false);

$schema = $field->getSchemaDataDefaults();
$this->assertSame('TestField[]', $schema['name']);
$this->assertFalse($schema['lazyLoad']);
$this->assertFalse($schema['creatable']);
$this->assertEquals([
['Title' => 'one', 'Value' => 'one'],
['Title' => 'two', 'Value' => 'two'],
], $schema['options']);

$field
->setShouldLazyLoad(true)
->setCanCreate(true);

$schema = $field->getSchemaDataDefaults();
$this->assertTrue($schema['lazyLoad']);
$this->assertTrue($schema['creatable']);
$this->assertContains('suggest', $schema['optionUrl']);
}

public function testSchemaIsAddedToAttributes()
{
$field = new StringTagField('TestField');
$attributes = $field->getAttributes();
$this->assertNotEmpty($attributes['data-schema']);
}

/**
* @param array $parameters
*
* @return HTTPRequest
*/
protected function getNewRequest(array $parameters)
Expand Down
6 changes: 3 additions & 3 deletions tests/Stub/TagFieldTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace SilverStripe\TagField\Tests\Stub;

use SilverStripe\Control\Controller;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\TagField\StringTagField;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\TagField\TagField;

class TagFieldTestController extends Controller implements TestOnly
{
Expand Down
83 changes: 60 additions & 23 deletions tests/TagFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\TagField\Tests;

use PHPUnit_Framework_TestCase;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
Expand Down Expand Up @@ -36,11 +37,11 @@ public function testItSavesLinksToNewTagsOnNewRecords()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue(array('Tag3', 'Tag4'));
$field->setValue(['Tag3', 'Tag4']);
$field->saveInto($record);
$record->write();
$this->compareExpectedAndActualTags(
array('Tag3', 'Tag4'),
['Tag3', 'Tag4'],
$record
);
}
Expand Down Expand Up @@ -91,7 +92,7 @@ public function testItSavesLinksToNewTagsOnExistingRecords()
$record->write();

$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue(array('Tag3', 'Tag4'));
$field->setValue(['Tag3', 'Tag4']);
$field->saveInto($record);

$this->compareExpectedAndActualTags(
Expand All @@ -105,13 +106,13 @@ public function testItSavesLinksToExistingTagsOnNewRecords()
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');

$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue(array('Tag1', 'Tag2'));
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);

$record->write();

$this->compareExpectedAndActualTags(
array('Tag1', 'Tag2'),
['Tag1', 'Tag2'],
$record
);
}
Expand All @@ -122,11 +123,11 @@ public function testItSavesLinksToExistingTagsOnExistingRecords()
$record->write();

$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue(array('Tag1', 'Tag2'));
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);

$this->compareExpectedAndActualTags(
array('Tag1', 'Tag2'),
['Tag1', 'Tag2'],
$record
);
}
Expand All @@ -142,29 +143,29 @@ public function testSaveDuplicateTags()

// Check tags before write
$this->compareExpectedAndActualTags(
array('Tag1', '222'),
['Tag1', '222'],
$record
);
$this->compareTagLists(
array('Tag1', '222'),
['Tag1', '222'],
TagFieldTestBlogTag::get()
);
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));

// Write new tags
$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue(array('222', 'Tag3'));
$field->setValue(['222', 'Tag3']);
$field->saveInto($record);

// Check only one new tag was added
$this->compareExpectedAndActualTags(
array('222', 'Tag3'),
['222', 'Tag3'],
$record
);

// Ensure that only one new dataobject was added and that tag2s id has not changed
$this->compareTagLists(
array('Tag1', '222', 'Tag3'),
['Tag1', '222', 'Tag3'],
TagFieldTestBlogTag::get()
);
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
Expand All @@ -177,7 +178,7 @@ public function testItSuggestsTags()
/**
* Partial tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag'));
$request = $this->getNewRequest(['term' => 'Tag']);

$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
Expand All @@ -187,7 +188,7 @@ public function testItSuggestsTags()
/**
* Exact tag title match.
*/
$request = $this->getNewRequest(array('term' => '222'));
$request = $this->getNewRequest(['term' => '222']);

$this->assertEquals(
'{"items":[{"id":"222","text":"222"}]}',
Expand All @@ -197,7 +198,7 @@ public function testItSuggestsTags()
/**
* Case-insensitive tag title match.
*/
$request = $this->getNewRequest(array('term' => 'TAG1'));
$request = $this->getNewRequest(['term' => 'TAG1']);

$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
Expand All @@ -207,7 +208,7 @@ public function testItSuggestsTags()
/**
* No tag title match.
*/
$request = $this->getNewRequest(array('term' => 'unknown'));
$request = $this->getNewRequest(['term' => 'unknown']);

$this->assertEquals(
'{"items":[]}',
Expand All @@ -226,7 +227,7 @@ public function testRestrictedSuggestions()
/**
* Partial tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag'));
$request = $this->getNewRequest(['term' => 'Tag']);

$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
Expand All @@ -236,7 +237,7 @@ public function testRestrictedSuggestions()
/**
* Exact tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag1'));
$request = $this->getNewRequest(['term' => 'Tag1']);

$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
Expand All @@ -246,7 +247,7 @@ public function testRestrictedSuggestions()
/**
* Excluded item doesn't appear in matches
*/
$request = $this->getNewRequest(array('term' => 'Tag2'));
$request = $this->getNewRequest(['term' => 'Tag2']);

$this->assertEquals(
'{"items":[]}',
Expand Down Expand Up @@ -274,7 +275,7 @@ public function testItDisplaysValuesFromRelations()
$record->write();

$form = new Form(
new TagFieldTestController($record),
new TagFieldTestController(),
'Form',
new FieldList(
$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class))
Expand Down Expand Up @@ -302,7 +303,7 @@ public function testItIgnoresNewTagsIfCannotCreate()

$tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first();

$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), array($tag->Title, 'Tag3'));
$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), [$tag->Title, 'Tag3']);
$field->setCanCreate(false);
$field->saveInto($record);

Expand All @@ -312,7 +313,7 @@ public function testItIgnoresNewTagsIfCannotCreate()
$record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID);

$this->compareExpectedAndActualTags(
array('Tag1'),
['Tag1'],
$record
);
}
Expand Down Expand Up @@ -350,6 +351,42 @@ public function testReadonlyTransformation()
{
$field = new TagField('Tags', '', TagFieldTestBlogTag::get());
$readOnlyField = $field->performReadonlyTransformation();
$this->assertEquals(ReadonlyTagField::class, get_class($readOnlyField));
$this->assertInstanceOf(ReadonlyTagField::class, $readOnlyField);
}

public function testGetSchemaDataDefaults()
{
$form = new Form(null, 'Form', new FieldList(), new FieldList());
$field = new TagField('TestField', 'Test Field', TagFieldTestBlogTag::get());
$field->setForm($form);

$field
->setShouldLazyLoad(false)
->setCanCreate(false);

$schema = $field->getSchemaDataDefaults();
$this->assertSame('TestField[]', $schema['name']);
$this->assertFalse($schema['lazyLoad']);
$this->assertFalse($schema['creatable']);
$this->assertEquals([
['Title' => 'Tag1', 'Value' => 'Tag1'],
['Title' => '222', 'Value' => '222'],
], $schema['options']);

$field
->setShouldLazyLoad(true)
->setCanCreate(true);

$schema = $field->getSchemaDataDefaults();
$this->assertTrue($schema['lazyLoad']);
$this->assertTrue($schema['creatable']);
$this->assertContains('suggest', $schema['optionUrl']);
}

public function testSchemaIsAddedToAttributes()
{
$field = new TagField('TestField');
$attributes = $field->getAttributes();
$this->assertNotEmpty($attributes['data-schema']);
}
}

0 comments on commit 8030ddf

Please # to comment.