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 4083a6b commit cc21e76
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 58 deletions.
94 changes: 52 additions & 42 deletions tests/php/Form/GridField/RichFilterHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,24 @@
use SilverStripe\ORM\DataList;
use SilverStripe\Security\SecurityToken;

/**
* Class RichFilterHeaderTest
* @package Terraformers\RichFilterHeader\Tests\Form\GridField
*/
class RichFilterHeaderTest extends SapphireTest
{
/**
* @var GridField
*/
protected $gridField;
protected ?GridField $gridField = null;

/**
* @var Form
*/
protected $form;
protected ?Form $form = null;

/**
* @var string
*/
protected static $fixture_file = 'RichFilterHeaderTest.yml';

/**
* @var array
*/
protected static $extra_dataobjects = array(
protected static $extra_dataobjects = [
Team::class,
Cheerleader::class,
CheerleaderHat::class,
);
];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -68,19 +55,19 @@ protected function setUp()
);
}

public function testCompositeFieldName()
public function testCompositeFieldName(): void
{
$gridFieldName = 'test-grid-field1';
$childFieldName = 'test-child-field1';
$compositeFieldName = RichFilterHeader::createCompositeFieldName($gridFieldName, $childFieldName);
$data = RichFilterHeader::parseCompositeFieldName($compositeFieldName);

$this->assertNotEmpty($data);
$this->assertEquals($gridFieldName, $data['grid_field']);
$this->assertEquals($childFieldName, $data['child_field']);
$this->assertEquals($gridFieldName, $data['grid_field'], 'We expect a specific GridField name');
$this->assertEquals($childFieldName, $data['child_field'], 'We expect a specific child field name');
}

public function testRenderFilteredHeaderStandard()
public function testRenderFilteredHeaderStandard(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand All @@ -93,11 +80,12 @@ public function testRenderFilteredHeaderStandard()
'<input type="text" name="filter[testfield][City]"'
. ' class="text grid-field__sort-field no-change-track form-group--no-label"'
. ' id="Form_mockform_filter_testfield_City" placeholder="Filter by City" />',
$htmlFragment['header']
$htmlFragment['header'],
'We expect a rendered filter'
);
}

public function testRenderFilterHeaderWithCustomFields()
public function testRenderFilterHeaderWithCustomFields(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand All @@ -115,18 +103,20 @@ public function testRenderFilterHeaderWithCustomFields()
'<select name="filter[testfield][Name]" '
. 'class="dropdown grid-field__sort-field no-change-track form-group--no-label"'
. ' id="Form_mockform_filter_testfield_Name" placeholder="Filter by Name">',
$htmlFragment['header']
$htmlFragment['header'],
'We expect a rendered Name filter'
);

$this->assertContains(
'<select name="filter[testfield][City]" '
. 'class="dropdown grid-field__sort-field no-change-track form-group--no-label"'
. ' id="Form_mockform_filter_testfield_City" placeholder="Filter by City">',
$htmlFragment['header']
$htmlFragment['header'],
'We expect a rendered City filter'
);
}

public function testRenderFilterHeaderWithFullConfig()
public function testRenderFilterHeaderWithFullConfig(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand Down Expand Up @@ -155,18 +145,20 @@ public function testRenderFilterHeaderWithFullConfig()
'<select name="filter[testfield][Name]" '
. 'class="dropdown grid-field__sort-field no-change-track form-group--no-label"'
. ' id="Form_mockform_filter_testfield_Name" placeholder="Filter by Name">',
$htmlFragment['header']
$htmlFragment['header'],
'We expect a rendered Name filter'
);

$this->assertContains(
'<select name="filter[testfield][City]" '
. 'class="dropdown grid-field__sort-field no-change-track form-group--no-label"'
. ' id="Form_mockform_filter_testfield_City" placeholder="Filter by City">',
$htmlFragment['header']
$htmlFragment['header'],
'We expect a rendered City filter'
);
}

public function testRenderFilterHeaderBasicFilter()
public function testRenderFilterHeaderBasicFilter(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand Down Expand Up @@ -213,11 +205,16 @@ public function testRenderFilterHeaderBasicFilter()
$gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
$list = $component->getManipulatedData($gridField, $gridField->getList());

$this->assertEquals(1, (int) $list->count());
$this->assertEquals($city, $list->first()->City);
$this->assertSame(
[
$city,
],
$list->column('City'),
'We expect a single item after filtering by City'
);
}

public function testRenderFilterHeaderAdvancedFilterAllKeywords()
public function testRenderFilterHeaderAdvancedFilterAllKeywords(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand Down Expand Up @@ -265,11 +262,16 @@ public function testRenderFilterHeaderAdvancedFilterAllKeywords()
$gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
$list = $component->getManipulatedData($gridField, $gridField->getList());

$this->assertEquals(1, (int) $list->count());
$this->assertEquals($keywords, $list->first()->Name);
$this->assertSame(
[
$keywords,
],
$list->column('Name'),
'We expect a single item after filtering by Name'
);
}

public function testRenderFilterHeaderAdvancedFilterManyManyRelation()
public function testRenderFilterHeaderAdvancedFilterManyManyRelation(): void
{
$gridField = $this->gridField;
$gridField->setList(DataList::create(Cheerleader::class));
Expand Down Expand Up @@ -320,11 +322,11 @@ public function testRenderFilterHeaderAdvancedFilterManyManyRelation()
$gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
$list = $component->getManipulatedData($gridField, $gridField->getList());

$this->assertEquals(1, (int) $list->count());
$this->assertEquals($hat->ID, $list->first()->Hats()->first()->ID);
$this->assertEquals(1, (int) $list->count(), 'We expect a single item after filtering');
$this->assertEquals($hat->ID, $list->first()->Hats()->first()->ID, 'We expect a specific result item');
}

public function testRenderFilterHeaderAdvancedFilterCustomCallback()
public function testRenderFilterHeaderAdvancedFilterCustomCallback(): void
{
$gridField = $this->gridField;
$config = $gridField->getConfig();
Expand Down Expand Up @@ -377,8 +379,16 @@ public function testRenderFilterHeaderAdvancedFilterCustomCallback()
/** @var DataList $list */
$list = $component->getManipulatedData($gridField, $gridField->getList());

$this->assertEquals(2, (int) $list->count());
$cities = $list->sort('City', 'ASC')->column('City');
$this->assertEquals(['newton', 'Wellington'], $cities);
$cities = $list
->sort('City', 'ASC')
->column('City');
$this->assertEquals(
[
'newton',
'Wellington'
],
$cities,
'We expect specific results after filtering'
);
}
}
32 changes: 16 additions & 16 deletions tests/php/Form/GridField/RichFilterHeaderTest.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\CheerleaderHat:
hat1:
Colour: Blue
Colour: 'Blue'
hat2:
Colour: Red
Colour: 'Red'
hat3:
Colour: Green
Colour: 'Green'
hat4:
Colour: Pink
Colour: 'Pink'
Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Cheerleader:
cheerleader1:
Name: Heather
Name: 'Heather'
Hats:
- =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\CheerleaderHat.hat2
cheerleader2:
Name: Bob
Name: 'Bob'
Hats:
- =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\CheerleaderHat.hat4
cheerleader3:
Name: Jenny
Name: 'Jenny'
Hats:
- =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\CheerleaderHat.hat1
cheerleader4:
Name: Sam
Name: 'Sam'
Hats:
- =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\CheerleaderHat.hat3

Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Team:
team1:
Name: Team 1
City: newton
Name: 'Team 1'
City: 'newton'
Cheerleader: =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Cheerleader.cheerleader3
team2:
Name: Team 2
City: Wellington
Name: 'Team 2'
City: 'Wellington'
Cheerleader: =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Cheerleader.cheerleader2
team3:
Name: Team 3
City: Auckland
Name: 'Team 3'
City: 'Auckland'
Cheerleader: =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Cheerleader.cheerleader4
team4:
Name: Team 4
City: Melbourne
Name: 'Team 4'
City: 'Melbourne'
Cheerleader: =>Terraformers\RichFilterHeader\Tests\Form\GridField\RichFilterHeaderTest\Cheerleader.cheerleader1

0 comments on commit cc21e76

Please sign in to comment.