Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make View trigger notice by default if elements are missing

and add elementExists() method
  • Loading branch information...
commit e1e4d931345ba1f7b4694af854e298b3389afa79 1 parent 4a6ebaa
Mark S. dereuromark authored
Showing with 65 additions and 12 deletions.
  1. +49 −8 lib/Cake/Test/Case/View/ViewTest.php
  2. +16 −4 lib/Cake/View/View.php
57 lib/Cake/Test/Case/View/ViewTest.php
View
@@ -623,6 +623,29 @@ public function testAddInlineScripts() {
}
/**
+ * testElementExists method
+ *
+ * @return void
+ */
+ public function testElementExists() {
+ $result = $this->View->elementExists('test_element');
+ $this->assertTrue($result);
+
+ $result = $this->View->elementExists('TestPlugin.plugin_element');
+ $this->assertTrue($result);
+
+ $result = $this->View->elementExists('non_existent_element');
+ $this->assertFalse($result);
+
+ $result = $this->View->elementExists('TestPlugin.element');
+ $this->assertFalse($result);
+
+ $this->View->plugin = 'TestPlugin';
+ $result = $this->View->elementExists('test_plugin_element');
+ $this->assertTrue($result);
+ }
+
+/**
* testElement method
*
* @return void
@@ -640,21 +663,39 @@ public function testElement() {
$result = $this->View->element('TestPlugin.plugin_element');
$this->assertEquals('this is the plugin element using params[plugin]', $result);
- $result = $this->View->element('test_plugin.plugin_element');
- $this->assertRegExp('/Not Found:/', $result);
- $this->assertRegExp('/test_plugin.plugin_element/', $result);
-
$this->View->plugin = 'TestPlugin';
$result = $this->View->element('test_plugin_element');
$this->assertEquals('this is the test set using View::$plugin plugin element', $result);
+ }
+/**
+ * testElementInexistent method
+ *
+ * @expectedException PHPUnit_Framework_Error_Notice
+ * @return void
+ */
+ public function testElementInexistent() {
$result = $this->View->element('non_existent_element');
- $this->assertRegExp('/Not Found:/', $result);
- $this->assertRegExp('/non_existent_element/', $result);
+ }
+/**
+ * testElementInexistent2 method
+ *
+ * @expectedException PHPUnit_Framework_Error_Notice
+ * @return void
+ */
+ public function testElementInexistent2() {
$result = $this->View->element('TestPlugin.plugin_element', array(), array('plugin' => 'test_plugin'));
- $this->assertRegExp('/Not Found:/', $result);
- $this->assertRegExp('/TestPlugin.plugin_element/', $result);
+ }
+
+/**
+ * testElementInexistent3 method
+ *
+ * @expectedException PHPUnit_Framework_Error_Notice
+ * @return void
+ */
+ public function testElementInexistent3() {
+ $result = $this->View->element('test_plugin.plugin_element');
}
/**
20 lib/Cake/View/View.php
View
@@ -385,6 +385,7 @@ public function getEventManager() {
* - `plugin` - Load an element from a specific plugin. This option is deprecated, see below.
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
* Defaults to false.
+ * - `ignoreMissing` - Used to allow missing elements. Set to true to not trigger notices.
* @return string Rendered Element
* @deprecated The `$options['plugin']` is deprecated and will be removed in CakePHP 3.0. Use
* `Plugin.element_name` instead.
@@ -412,14 +413,25 @@ public function element($name, $data = array(), $options = array()) {
return $this->_renderElement($file, $data, $options);
}
- $file = 'Elements' . DS . $name . $this->ext;
-
- if (Configure::read('debug') > 0) {
- return __d('cake_dev', 'Element Not Found: %s', $file);
+ if (empty($options['ignoreMissing'])) {
+ $file = 'Elements' . DS . $name . $this->ext;
+ trigger_error(__d('cake_dev', 'Element Not Found: %s', $file), E_USER_NOTICE);
}
}
/**
+ * Checks if an element exists
+ *
+ * @param string $name Name of template file in the/app/View/Elements/ folder,
+ * or `MyPlugin.template` to use the template element from MyPlugin. If the element
+ * is not found in the plugin, the normal view path cascade will be searched.
+ * @return boolean Success
+ */
+ public function elementExists($name) {
+ return (bool)$this->_getElementFilename($name);
+ }
+
+/**
* Renders view for given view file and layout.
*
* Render triggers helper callbacks, which are fired before and after the view are rendered,
Please sign in to comment.
Something went wrong with that request. Please try again.