Permalink
Browse files

Merge pull request #994 from dereuromark/2.3-missing-elements

make View trigger notice if elements are missing.
  • Loading branch information...
2 parents dc37082 + d4af674 commit dfb154464ef5a30fcb0ace10f645d4d487a4d6d0 @lorenzo lorenzo committed Dec 1, 2012
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
@@ -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');
}
/**
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 check 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,

0 comments on commit dfb1544

Please sign in to comment.