Browse Source

Allows setting of additional valid view options
closes #4582

Andrew Lechowicz 11 years ago
parent
commit
50e75aa64b
2 changed files with 89 additions and 0 deletions
  1. 25 0
      src/View/ViewVarsTrait.php
  2. 64 0
      tests/TestCase/View/ViewVarsTraitTest.php

+ 25 - 0
src/View/ViewVarsTrait.php

@@ -71,4 +71,29 @@ trait ViewVarsTrait {
 		$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);
+	}
 }

+ 64 - 0
tests/TestCase/View/ViewVarsTraitTest.php

@@ -72,4 +72,68 @@ class ViewVarsTraitTest extends TestCase {
 		$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));
+	}
+
 }