Skip to content

Commit

Permalink
Add tests for ElementalAreasExtension::getElementalTypes and minor sy…
Browse files Browse the repository at this point in the history
…ntax tidy up
  • Loading branch information
robbieaverill committed Jan 16, 2019
1 parent 405a691 commit e80875f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/ElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(BaseElement $element)
}

/**
* @return Element
* @return BaseElement
*/
public function getElement()
{
Expand Down
17 changes: 12 additions & 5 deletions src/Extensions/ElementalAreasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,29 @@
class ElementalAreasExtension extends DataExtension
{
/**
* Classes to ignore adding elements to
* @config
*
* @var array $ignored_classes Classes to ignore adding elements too.
* @var array $ignored_classes
*/
private static $ignored_classes = [];

/**
* @config
*
* On saving the element area, should Elemental reset the main website
* `$Content` field.
*
* @config
* @var boolean
*/
private static $clear_contentfield = false;

/**
* Whether to sort the elements alphabetically by their title
*
* @config
* @var boolean
*/
private static $sort_types_alphabetically = true;

/**
* Get the available element types for this page type,
*
Expand Down Expand Up @@ -87,7 +94,7 @@ public function getElementalTypes()
} else {
$disallowedElements = (array) $config->get('disallowed_elements');
}
$list = array();
$list = [];

foreach ($availableClasses as $availableClass) {
/** @var BaseElement $inst */
Expand Down
57 changes: 57 additions & 0 deletions tests/Extensions/ElementalAreasExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace DNADesign\Elemental\Tests\Extensions;

use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use DNADesign\Elemental\Tests\Src\TestElement;
use DNADesign\Elemental\Tests\Src\TestUnusedElement;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;

class ElementalAreasExtensionTest extends SapphireTest
{
protected static $required_extensions = [
SiteTree::class => [
ElementalAreasExtension::class,
],
];

protected static $extra_dataobjects = [
TestElement::class,
TestUnusedElement::class,
];

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

$this->logInWithPermission('ADMIN');

SiteTree::config()
->set('allowed_elements', null)
->set('disallowed_elements', []);
}

public function testGetElementalTypesSortsAlphabetically()
{
SiteTree::config()->set('sort_types_alphabetically', true);

/** @var SiteTree|ElementalAreasExtension $page */
$page = new SiteTree();
$types = $page->getElementalTypes();

$this->assertSame(['A test element', 'Content', 'Unused Element'], array_values($types));
}

public function testGetElementalTypesAreNotSortedAlphabetically()
{
SiteTree::config()->set('sort_types_alphabetically', false);

/** @var SiteTree|ElementalAreasExtension $page */
$page = new SiteTree();
$types = $page->getElementalTypes();

$this->assertSame(['Content', 'A test element', 'Unused Element'], array_values($types));
}
}

0 comments on commit e80875f

Please sign in to comment.