1.20.x common.test CommonBackdropRenderTestCase::testBackdropRenderChildrenAttached()

Test #attached functionality in children elements.

File

modules/simpletest/tests/common.test, line 1989
Tests for common.inc functionality.

Class

CommonBackdropRenderTestCase
Tests for backdrop_render().

Code

function testBackdropRenderChildrenAttached() {
  // The cache system is turned off for POST requests.
  $request_method = $_SERVER['REQUEST_METHOD'];
  $_SERVER['REQUEST_METHOD'] = 'GET';

  // Create an element with a child and subchild.  Each element loads a
  // different JavaScript file using #attached.
  $parent_js = backdrop_get_path('module', 'user') . '/js/user.js';
  $child_js = backdrop_get_path('module', 'node') . '/js/node.js';
  $subchild_js = backdrop_get_path('module', 'book') . '/js/book.js';

  $element = array(
    '#type' => 'fieldset',
    '#cache' => array(
      'keys' => array('simpletest', 'backdrop_render', 'children_attached'),
    ),
    '#attached' => array('js' => array($parent_js)),
    '#title' => 'Parent',
  );
  $element['child'] = array(
    '#type' => 'fieldset',
    '#attached' => array('js' => array($child_js)),
    '#title' => 'Child',
  );
  $element['child']['subchild'] = array(
    '#attached' => array('js' => array($subchild_js)),
    '#markup' => 'Subchild',
  );

  // Render the element and verify the presence of #attached JavaScript.
  backdrop_render($element);
  $scripts = backdrop_get_js();
  $this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included.');
  $this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included.');
  $this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included.');

  // Load the element from cache and verify the presence of the #attached
  // JavaScript.
  backdrop_static_reset('backdrop_add_js');
  $this->assertTrue(backdrop_render_cache_get($element), 'The element was retrieved from cache.');
  $scripts = backdrop_get_js();
  $this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included when loading from cache.');
  $this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included when loading from cache.');
  $this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included when loading from cache.');

  $_SERVER['REQUEST_METHOD'] = $request_method;
}