Skip to content

Commit

Permalink
Change: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Nov 6, 2021
1 parent d10e4ef commit 257c998
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 96 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# HasOne Selector

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/6e6bbf47-1ea0-4169-94fb-850bf9baccb1/mini.png)](https://insight.sensiolabs.com/projects/6e6bbf47-1ea0-4169-94fb-850bf9baccb1)
[![Build Status](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/badges/build.png?b=master)](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/build-status/master)
[![Code Coverage](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/?branch=master)
[![Build Status](https://travis-ci.org/satrun77/silverstripe-hasoneselector.svg?branch=master)](https://travis-ci.org/satrun77/silverstripe-hasoneselector)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/satrun77/silverstripe-hasoneselector/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/moo/hasoneselector/v/stable?format=flat)](https://packagist.org/packages/moo/hasoneselector)
[![License](https://poser.pugx.org/moo/hasoneselector/license?format=flat)](https://packagist.org/packages/moo/hasoneselector)
Expand Down
296 changes: 249 additions & 47 deletions tests/HasOneSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,58 @@

namespace Moo\HasOneSelector\Tests;

use InvalidArgumentException;
use Moo\HasOneSelector\Extension\GridFieldItemRequest;
use Moo\HasOneSelector\Form\Field;
use Moo\HasOneSelector\Tests\Object\ControllerTest;
use Moo\HasOneSelector\Form\GridField;
use Moo\HasOneSelector\ORM\DataList;
use Moo\HasOneSelector\Tests\Object\PageTest;
use Moo\HasOneSelector\Tests\Object\ResourceTest;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;

/**
* @internal
* @coversNothing
*/
class HasOneSelectorTest extends FunctionalTest
class HasOneSelectorTest extends SapphireTest
{
protected static $fixture_file = 'HasOneSelectorTest.yml';

protected static $extra_dataobjects = [
PageTest::class,
ResourceTest::class,
];
public static function tearDownAfterClass()
{
// Disable teardown to prevent db access
}

public function testNoItemSelected()
public function testNoItemSelected(): void
{
$page = $this->getPage('page-1');
$field = $this->getField($page)->setEmptyString('No data selected');
$form = new Form(new ControllerTest(), 'Form', FieldList::create($field), FieldList::create());
$html = $field->FieldHolder();
RequestHandler::config()->set('url_segment', '/');
$page = $this->getPage()->setComponent('Resource', null);
$field = $this->getField($page);
$field->setEmptyString('No data selected');
new Form(new RequestHandler(), 'Form', FieldList::create($field), FieldList::create());
$html = $field->FieldHolder();

$this->assertContains('No data selected', $html);
self::assertContains('No data selected', $html);
$this->assertEquals(ResourceTest::class, $field->getDataClass());
$this->assertFalse($field->getRecord()->exists());
$this->assertNull($field->getRecord());
}

public function testSelectedItem()
public function testSelectedItem(): void
{
$page = $this->getPage('page-1');
$field = $this->getField($page)->setEmptyString('No data selected');
$resource = $this->getResource('resource-1');
$resource = $this->getResource();
$page = $this->getPage();
$page->setField('ResourceID', $resource->ID);
$form = new Form(Controller::curr(), 'Form', FieldList::create($field), FieldList::create());
$page->setComponent('Resource', $resource);

$field = $this->getField($page);
$field->getGridField()->getList()->setList([
$this->getResource(),
$this->getResource()->setField('ID', 99),
]);
$resource = $page->Resource();

$this->assertInstanceOf(ResourceTest::class, $field->getRecord());
$this->assertInstanceOf(PageTest::class, $field->getOwner());
Expand All @@ -51,56 +62,247 @@ public function testSelectedItem()
$this->assertEquals($resource->Title, $field->getColumnContent($field->getRecord(), 'Title'));
}

public function testSelectingRemovingItem()
public function testSelectingRemovingItem(): void
{
$page = $this->getPage('page-1');
RequestHandler::config()->set('url_segment', '/');
$resource = $this->getResource()->update([
'ID' => 88,
]);
$page = $this->getPage();
$field = $this->getField($page);

$resource = $this->getResource('resource-1');
$form = new Form(new RequestHandler(), 'Form', FieldList::create($field), FieldList::create());
$field->setForm($form);
$field->getList()->add($resource);
$form = new Form(new ControllerTest(), 'Form', FieldList::create($field), FieldList::create());
$page->setComponent('Resource', $resource);

$this->assertInstanceOf(ResourceTest::class, $field->getRecord());
$this->assertInstanceOf(PageTest::class, $field->getOwner());
$this->assertEquals($field->getList()->first()->ID, $resource->ID);
$this->assertEquals($field->getList(), $field->getManipulatedList());
$this->assertEquals($resource->Title, $field->getColumnContent($field->getRecord(), 'Title'));
$this->assertEquals($resource->Title, $field->getGridField()->getColumnContent($field->getGridField()->getRecord(), 'Title'));

$field->getList()->remove($resource);
$html = $field->FieldHolder();

$this->assertContains('No resource test selected', $html);
$this->assertEquals(ResourceTest::class, $field->getDataClass());
$this->assertFalse($field->getRecord()->exists());
}

public function testManageLinkableComponent(): void
{
// Get HasOne Field
$field = $this->getField($this->getPage());

// Remove linkable component and assert it is removed
$components = $field->removeLinkable()->getConfig()->getComponents()->toArray();
$componentClasses = array_map('get_class', $components);
$this->assertNotContains(GridFieldAddExistingAutocompleter::class, $componentClasses);

// ENable linkable and assert it exists
$components = $field->enableLinkable()->getConfig()->getComponents()->toArray();
$componentClasses = array_map('get_class', $components);
$this->assertContains(GridFieldAddExistingAutocompleter::class, $componentClasses);
}

public function testManageAddableComponent(): void
{
// Get HasOne Field
$field = $this->getField($this->getPage());

// Remove linkable component and assert it is removed
$components = $field->removeAddable()->getConfig()->getComponents()->toArray();
$componentClasses = array_map('get_class', $components);
$this->assertNotContains(GridFieldAddNewButton::class, $componentClasses);

// ENable linkable and assert it exists
$components = $field->enableAddable()->getConfig()->getComponents()->toArray();
$componentClasses = array_map('get_class', $components);
$this->assertContains(GridFieldAddNewButton::class, $componentClasses);
}

public function testUpdateGridFieldDisplayFields(): void
{
// Get HasOne Field
$field = $this->getField($this->getPage());

$fieldsToDisplay = [
'One' => 'One',
'Title' => 'Title',
];
$field->setDisplayFields($fieldsToDisplay);

foreach ($fieldsToDisplay as $name) {
$fields = $field->getGridField()->getColumnMetadata($name);
$this->assertArrayHasKey('title', $fields);
}

try {
$field->getGridField()->getColumnMetadata('NotColumn');
} catch (InvalidArgumentException $exception) {
$this->assertEquals('Bad column "NotColumn"', $exception->getMessage());
}
}

public function testUpdateGridFieldFieldFormatting(): void
{
// Get HasOne Field
$page = $this->getPage();
$field = $this->getField($page);

$formatting = [
'Title' => static function ($val) {
return 'Cool '.$val;
},
];
$field->setFieldFormatting($formatting);

$title = $field->getGridField()->getColumnContent($page, 'Title');
$this->assertEquals($formatting['Title']($page->Title), $title);
}

public function testLoadDataObjectFromSession(): void
{
$session = Controller::curr()->getRequest()->getSession();
$page = $this->getPage();

$resource = $this->getResource()->update([
'ID' => 99,
]);

$session->set(GridField::formatSessionName($page), [
'Relation' => 'ResourceID',
'RelationID' => $resource->ID,
]);

$field = $this->getField($page);

$filterArguments = $field->getList()->getFilterArgs();
$this->assertEquals($resource->ID, $filterArguments[1]);

$session->set(GridField::formatSessionName($page), []);

$resource->ID = 1;
$page = $this->getPage();
$page->setField('ResourceID', $resource->ID);
$page->setComponent('Resource', $resource);

$field = $this->getField($page);

$this->assertInstanceOf(ResourceTest::class, $field->getRecord());
$this->assertEquals($resource->Title, $field->getRecord()->Title);

$filterArguments = $field->getGridField()->getList()->getFilterArgs();
$this->assertEquals($resource->ID, $filterArguments[1]);
}

public function testBreadcrumb(): void
{
// Get item request extension
$extension = $this->getGridFieldItemRequest();

// Function to generate a link item
$linkData = static function ($title = 'Link 1', $link = '/') {
return ArrayData::create([
'Title' => $title,
'Link' => $link,
]);
};
// List of items for breadcrumb
$list = new ArrayList([
$linkData(),
$breadcrumbItem = $linkData('New Test', ''),
]);
// Execute hook to update breadcrumb
$extension->updateBreadcrumbs($list);

// Assert that the breadcrumb item updated to match extension owner
$this->assertEquals(
$extension->getOwner()->Link(),
$breadcrumbItem->Link
);
}

public function testAfterSave(): void
{
// Instance of unsaved data object (page)
$page = $this->getPage()->update(['ID' => 0]);

// Get item request extension
$extension = $this->getGridFieldItemRequest();

// Set session value
$session = Controller::curr()->getRequest()->getSession();
$session->set(GridField::formatSessionName($page), [
'Relation' => 'ResourceID',
'RelationID' => 1,
]);

// Execute after save hook
$extension->onAfterSave($page);

// Assert that the session value is cleared
$this->assertNull($session->get(GridField::formatSessionName($page)));
}

/**
* @param $name
*
* @return PageTest
* Get instance of grid field item request with owner defined.
*/
protected function getPage($name)
protected function getGridFieldItemRequest(): GridFieldItemRequest
{
return $this->objFromFixture(PageTest::class, $name);
$extension = new GridFieldItemRequest();
$extension->setOwner(new class() {
public $record;

public function __construct()
{
$this->record = new class() {
public function i18n_singular_name()
{
return 'Test';
}
};
}

public function Link()
{
return '/TextLink';
}
});

return $extension;
}

/**
* Get instance of owner data object (Page).
*/
protected function getPage(): PageTest
{
return new PageTest();
}

/**
* @param $name
*
* @return ResourceTest
* Get instance of data object (Resource).
*/
protected function getResource($name)
protected function getResource(): ResourceTest
{
return $this->objFromFixture(ResourceTest::class, $name);
return ResourceTest::create([
'ID' => 1,
'Title' => 'Resource 1',
]);
}

/**
* @param mixed $page
*
* @return Field
* Get instance of field for page test data object.
*/
protected function getField($page)
protected function getField(PageTest $page): Field
{
return Field::create('Resource', 'Resource', $page, ResourceTest::class);
// Replace module DataList with test subclass
Injector::inst()->load([
DataList::class => [
'class' => Object\DataList::class,
],
]);

return new Field('Resource', 'Resource', $page, ResourceTest::class);
}
}
25 changes: 0 additions & 25 deletions tests/HasOneSelectorTest.yml

This file was deleted.

Loading

0 comments on commit 257c998

Please sign in to comment.