You are here

class ElementTest in Zircon Profile 8

Same name in this branch
  1. 8 vendor/behat/mink/tests/Element/ElementTest.php \Behat\Mink\Tests\Element\ElementTest
  2. 8 core/tests/Drupal/Tests/Core/Render/ElementTest.php \Drupal\Tests\Core\Render\ElementTest
  3. 8 core/modules/system/src/Tests/Form/ElementTest.php \Drupal\system\Tests\Form\ElementTest
Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Render/ElementTest.php \Drupal\Tests\Core\Render\ElementTest

@coversDefaultClass \Drupal\Core\Render\Element @group Render

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of ElementTest

File

core/tests/Drupal/Tests/Core/Render/ElementTest.php, line 18
Contains \Drupal\Tests\Core\Render\ElementTest.

Namespace

Drupal\Tests\Core\Render
View source
class ElementTest extends UnitTestCase {

  /**
   * Tests the property() method.
   */
  public function testProperty() {
    $this
      ->assertTrue(Element::property('#property'));
    $this
      ->assertFalse(Element::property('property'));
    $this
      ->assertFalse(Element::property('property#'));
  }

  /**
   * Tests the properties() method.
   */
  public function testProperties() {
    $element = array(
      '#property1' => 'property1',
      '#property2' => 'property2',
      'property3' => 'property3',
    );
    $properties = Element::properties($element);
    $this
      ->assertContains('#property1', $properties);
    $this
      ->assertContains('#property2', $properties);
    $this
      ->assertNotContains('property3', $properties);
  }

  /**
   * Tests the child() method.
   */
  public function testChild() {
    $this
      ->assertFalse(Element::child('#property'));
    $this
      ->assertTrue(Element::child('property'));
    $this
      ->assertTrue(Element::child('property#'));
  }

  /**
   * Tests the children() method.
   */
  public function testChildren() {
    $element = array(
      'child2' => array(
        '#weight' => 10,
      ),
      'child1' => array(
        '#weight' => 0,
      ),
      'child3' => array(
        '#weight' => 20,
      ),
      '#property' => 'property',
    );
    $expected = array(
      'child2',
      'child1',
      'child3',
    );
    $element_copy = $element;
    $this
      ->assertSame($expected, Element::children($element_copy));

    // If #sorted is already set, no sorting should happen.
    $element_copy = $element;
    $element_copy['#sorted'] = TRUE;
    $expected = array(
      'child2',
      'child1',
      'child3',
    );
    $this
      ->assertSame($expected, Element::children($element_copy, TRUE));

    // Test with weight sorting, #sorted property should be added.
    $expected = array(
      'child1',
      'child2',
      'child3',
    );
    $element_copy = $element;
    $this
      ->assertSame($expected, Element::children($element_copy, TRUE));
    $this
      ->assertArrayHasKey('#sorted', $element_copy);
    $this
      ->assertTrue($element_copy['#sorted']);

    // The order should stay the same if no weights present.
    $element_no_weight = array(
      'child2' => array(),
      'child1' => array(),
      'child3' => array(),
      '#property' => 'property',
    );
    $expected = array(
      'child2',
      'child1',
      'child3',
    );
    $this
      ->assertSame($expected, Element::children($element_no_weight, TRUE));

    // The order of children with same weight should be preserved.
    $element_mixed_weight = array(
      'child5' => array(
        '#weight' => 10,
      ),
      'child3' => array(
        '#weight' => -10,
      ),
      'child1' => array(),
      'child4' => array(
        '#weight' => 10,
      ),
      'child2' => array(),
    );
    $expected = array(
      'child3',
      'child1',
      'child2',
      'child5',
      'child4',
    );
    $this
      ->assertSame($expected, Element::children($element_mixed_weight, TRUE));
  }

  /**
   * Tests the children() method with an invalid key.
   *
   * @expectedException \PHPUnit_Framework_Error
   * @expectedExceptionMessage "foo" is an invalid render array key
   */
  public function testInvalidChildren() {
    $element = array(
      'foo' => 'bar',
    );
    Element::children($element);
  }

  /**
   * Tests the children() method with an ignored key/value pair.
   */
  public function testIgnoredChildren() {
    $element = array(
      'foo' => NULL,
    );
    $this
      ->assertSame(array(), Element::children($element));
  }

  /**
   * Tests the visibleChildren() method.
   *
   * @param array $element
   *   The test element array.
   * @param array $expected_keys
   *   The expected keys to be returned from Element::getVisibleChildren().
   *
   * @dataProvider providerVisibleChildren
   */
  public function testVisibleChildren(array $element, array $expected_keys) {
    $this
      ->assertSame($expected_keys, Element::getVisibleChildren($element));
  }

  /**
   * Data provider for testVisibleChildren.
   *
   * @return array
   */
  public function providerVisibleChildren() {
    return array(
      array(
        array(
          '#property1' => '',
          '#property2' => array(),
        ),
        array(),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
        ),
        array(
          'child1',
        ),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#access' => TRUE,
          ),
        ),
        array(
          'child1',
          'child2',
        ),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#access' => FALSE,
          ),
        ),
        array(
          'child1',
        ),
      ),
      'access_result_object_allowed' => array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#access' => AccessResult::allowed(),
          ),
        ),
        array(
          'child1',
          'child2',
        ),
      ),
      'access_result_object_forbidden' => array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#access' => AccessResult::forbidden(),
          ),
        ),
        array(
          'child1',
        ),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#type' => 'textfield',
          ),
        ),
        array(
          'child1',
          'child2',
        ),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#type' => 'value',
          ),
        ),
        array(
          'child1',
        ),
      ),
      array(
        array(
          '#property1' => '',
          'child1' => array(),
          'child2' => array(
            '#type' => 'hidden',
          ),
        ),
        array(
          'child1',
        ),
      ),
    );
  }

  /**
   * Tests the setAttributes() method.
   *
   * @dataProvider providerTestSetAttributes
   */
  public function testSetAttributes($element, $map, $expected_element) {
    Element::setAttributes($element, $map);
    $this
      ->assertSame($expected_element, $element);
  }

  /**
   * Data provider for testSetAttributes().
   */
  public function providerTestSetAttributes() {
    $base = array(
      '#id' => 'id',
      '#class' => array(),
    );
    return array(
      array(
        $base,
        array(),
        $base,
      ),
      array(
        $base,
        array(
          'id',
          'class',
        ),
        $base + array(
          '#attributes' => array(
            'id' => 'id',
            'class' => array(),
          ),
        ),
      ),
      array(
        $base + array(
          '#attributes' => array(
            'id' => 'id-not-overwritten',
          ),
        ),
        array(
          'id',
          'class',
        ),
        $base + array(
          '#attributes' => array(
            'id' => 'id-not-overwritten',
            'class' => array(),
          ),
        ),
      ),
    );
  }

  /**
   * @covers ::isEmpty
   *
   * @dataProvider providerTestIsEmpty
   */
  public function testIsEmpty(array $element, $expected) {
    $this
      ->assertSame(Element::isEmpty($element), $expected);
  }
  public function providerTestIsEmpty() {
    return [
      [
        [],
        TRUE,
      ],
      [
        [
          '#cache' => [],
        ],
        TRUE,
      ],
      [
        [
          '#cache' => [
            'tags' => [
              'foo',
            ],
          ],
        ],
        TRUE,
      ],
      [
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
          ],
        ],
        TRUE,
      ],
      [
        [
          '#cache' => [],
          '#markup' => 'llamas are awesome',
        ],
        FALSE,
      ],
      [
        [
          '#markup' => 'llamas are the most awesome ever',
        ],
        FALSE,
      ],
      [
        [
          '#cache' => [],
          '#any_other_property' => TRUE,
        ],
        FALSE,
      ],
      [
        [
          '#any_other_property' => TRUE,
        ],
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ElementTest::providerTestIsEmpty public function
ElementTest::providerTestSetAttributes public function Data provider for testSetAttributes().
ElementTest::providerVisibleChildren public function Data provider for testVisibleChildren.
ElementTest::testChild public function Tests the child() method.
ElementTest::testChildren public function Tests the children() method.
ElementTest::testIgnoredChildren public function Tests the children() method with an ignored key/value pair.
ElementTest::testInvalidChildren public function Tests the children() method with an invalid key.
ElementTest::testIsEmpty public function @covers ::isEmpty
ElementTest::testProperties public function Tests the properties() method.
ElementTest::testProperty public function Tests the property() method.
ElementTest::testSetAttributes public function Tests the setAttributes() method.
ElementTest::testVisibleChildren public function Tests the visibleChildren() method.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259