Skip to content

Commit

Permalink
make View trigger notice by default if elements are missing
Browse files Browse the repository at this point in the history
and add elementExists() method
  • Loading branch information
euromark committed Nov 30, 2012
1 parent 4a6ebaa commit e1e4d93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
57 changes: 49 additions & 8 deletions lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -622,6 +622,29 @@ public function testAddInlineScripts() {
$this->assertEquals(array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'), $View->scripts());
}

/**
* 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
*
Expand All @@ -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');
}

/**
Expand Down
20 changes: 16 additions & 4 deletions lib/Cake/View/View.php
Expand Up @@ -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.
Expand Down Expand Up @@ -412,13 +413,24 @@ 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.
*
Expand Down

0 comments on commit e1e4d93

Please sign in to comment.