Skip to content
Permalink
Browse files

Allows setting of additional valid view options

closes #4582
  • Loading branch information...
alecho committed Sep 13, 2014
1 parent a1ffb7c commit 50e75aa64bcc6cbf90bc778e9ef0624ff2d66116
Showing with 89 additions and 0 deletions.
  1. +25 −0 src/View/ViewVarsTrait.php
  2. +64 −0 tests/TestCase/View/ViewVarsTraitTest.php
@@ -71,4 +71,29 @@ public function set($name, $val = null) {
$this->viewVars = $data + $this->viewVars;
}
/**
* Get/Set valid view options in the object's _validViewOptions property. The proptery is
* created as an empty array if it is not set. If called without any parameters it will
* return the current list of valid view options. See `createView()`.
*
* @param string|array $options string or array of string to be appended to _validViewOptions.
* @param bool $merge Whether to merge with or override existing valid View options.
* Defaults to `true`.
* @return array The updated view options as an array.
*/
public function viewOptions($options = null, $merge = true) {
if (!isset($this->_validViewOptions)) {
$this->_validViewOptions = [];
}
if ($options == null) {
return $this->_validViewOptions;
}
if (!$merge) {
return $this->_validViewOptions = (array)$options;
}
return $this->_validViewOptions = array_merge($this->_validViewOptions, (array)$options);
}
}
@@ -72,4 +72,68 @@ public function testSetTwoParamCombind() {
$this->assertEquals($expected, $this->subject->viewVars);
}
/**
* test viewOptions() with 1 string param, merge true
*
* @return void
*/
public function testAddOneViewOption() {
$option = 'newOption';
$this->subject->viewOptions($option);
$this->assertContains($option, $this->subject->_validViewOptions);
}
/**
* test viewOptions() with 2 strings in array, merge true.
*
* @return void
*/
public function testAddTwoViewOption() {
$this->subject->_validViewOptions = ['oldOption'];
$option = ['newOption', 'anotherOption'];
$result = $this->subject->viewOptions($option);
$expects = ['oldOption', 'newOption', 'anotherOption'];
$this->assertContainsOnly('string', $result);
$this->assertEquals($expects, $result);
}
/**
* test empty params reads _validViewOptions.
*
* @return void
*/
public function testReadingViewOptions() {
$expected = $this->subject->_validViewOptions = ['one', 'two', 'three'];
$result = $this->subject->viewOptions();
$this->assertEquals($expected, $result);
}
/**
* test setting $merge `false` overrides currect options.
*
* @return void
*/
public function testMergeFalseViewOptions() {
$this->subject->_validViewOptions = ['one', 'two', 'three'];
$expected = ['four', 'five', 'six'];
$result = $this->subject->viewOptions($expected, false);
$this->assertEquals($expected, $result);
}
/**
* test _validViewOptions is undefined and $opts is null, an empty array is returned.
*
* @return void
*/
public function testUndefinedValidViewOptions() {
$result = $this->subject->viewOptions();
$this->assertTrue(is_array($result));
$this->assertTrue(empty($resulit));
}
}

0 comments on commit 50e75aa

Please sign in to comment.
You can’t perform that action at this time.