Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
[TASK] Refactor and cover ContentSelector with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Feb 5, 2015
1 parent e181c81 commit 224e80c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
18 changes: 14 additions & 4 deletions Classes/Backend/ContentSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class ContentSelector {
* @return string
*/
public function renderField(array &$parameters, &$parentObject) {
/** @var ConfigurationService $contentService */
$contentService = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager')->get('FluidTYPO3\Fluidcontent\Service\ConfigurationService');
$contentService = $this->getConfigurationService();
$setup = $contentService->getContentElementFormInstances();
$name = $parameters['itemFormElName'];
$value = $parameters['itemFormElValue'];
Expand All @@ -37,8 +36,9 @@ public function renderField(array &$parameters, &$parentObject) {
$optionValue = $form->getOption('contentElementId');
$selected = ($optionValue === $value ? ' selected="selected"' : '');
$label = $form->getLabel();
$label = $GLOBALS['LANG']->sL($label);
$select .= '<option value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($label) . '</option>' . LF;
$label = (0 === strpos($label, 'LLL:') ? $GLOBALS['LANG']->sL($label) : $label);;
$select .= '<option value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' .
htmlspecialchars($label) . '</option>' . LF;
}
$select .= '</optgroup>' . LF;
}
Expand All @@ -47,4 +47,14 @@ public function renderField(array &$parameters, &$parentObject) {
return $select;
}

/**
* @return ConfigurationService
*/
protected function getConfigurationService() {
/** @var ConfigurationService $contentService */
$contentService = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager')
->get('FluidTYPO3\Fluidcontent\Service\ConfigurationService');
return $contentService;
}

}
53 changes: 44 additions & 9 deletions Tests/Unit/Backend/ContentSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

use FluidTYPO3\Fluidcontent\Backend\ContentSelector;
use FluidTYPO3\Flux\Form;
use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -17,6 +18,21 @@
*/
class ContentSelectorTest extends UnitTestCase {

/**
* @return void
*/
public function setUp() {
$GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array('sL'));
$statement = $this->getMock('TYPO3\\CMS\\Core\\Database\\PreparedStatement', array('execute', 'free', 'fetch'), array(), '', FALSE);
$statement->expects($this->any())->method('execute')->willReturn(FALSE);
$statement->expects($this->any())->method('fetch')->willReturn(FALSE);
$statement->expects($this->any())->method('free');
$GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection',
array('exec_SELECTquery', 'prepare_SELECTquery'), array(), '', FALSE);
$GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTquery')->willReturn(FALSE);
$GLOBALS['TYPO3_DB']->expects($this->any())->method('prepare_SELECTquery')->willReturn($statement);
}

/**
* @return void
*/
Expand All @@ -30,15 +46,6 @@ public function testCreatesInstance() {
* @return void
*/
public function testRenderFieldCreatesSelectTag() {
$GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array('sL'));
$statement = $this->getMock('TYPO3\\CMS\\Core\\Database\\PreparedStatement', array('execute', 'free', 'fetch'), array(), '', FALSE);
$statement->expects($this->any())->method('execute')->willReturn(FALSE);
$statement->expects($this->any())->method('fetch')->willReturn(FALSE);
$statement->expects($this->any())->method('free');
$GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection',
array('exec_SELECTquery', 'prepare_SELECTquery'), array(), '', FALSE);
$GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTquery')->willReturn(FALSE);
$GLOBALS['TYPO3_DB']->expects($this->any())->method('prepare_SELECTquery')->willReturn($statement);
$instance = new ContentSelector();
$parameters = array(
'itemFormElName' => 'foobar',
Expand All @@ -50,4 +57,32 @@ public function testRenderFieldCreatesSelectTag() {
$this->assertContains($parameters['itemFormElName'], $rendered);
}

/**
* @return void
*/
public function testRenderFieldCreatesExpectedOptions() {
$configurationService = $this->getMock(
'FluidTYPO3\\Fluidcontent\\Service\\ConfigurationService',
array('getContentElementFormInstances')
);
$forms = array(
'myextension' => array(
Form::create(array('id' => 'test1', 'label' => 'Test1', 'options' => array('contentElementId' => 'test1'))),
Form::create(array('id' => 'test2', 'label' => 'Test2', 'options' => array('contentElementId' => 'test2')))
)
);
$parameters = array(
'itemFormElName' => 'foobar',
'itemFormElValue' => 'foovalue'
);
$parent = 'unused';
$configurationService->expects($this->once())->method('getContentElementFormInstances')->willReturn($forms);
$instance = $this->getMock('FluidTYPO3\\Fluidcontent\\Backend\\ContentSelector', array('getConfigurationService'));
$instance->expects($this->once())->method('getConfigurationService')->willReturn($configurationService);
$rendered = $instance->renderField($parameters, $parent);
$this->assertContains('<optgroup label="myextension">', $rendered);
$this->assertContains('<option value="test1">Test1</option>', $rendered);
$this->assertContains('<option value="test2">Test2</option>', $rendered);
}

}

0 comments on commit 224e80c

Please sign in to comment.