You are here

public function BricksTest::testBricks in Bricks​ 8

Same name in this branch
  1. 8 tests/src/Functional/BricksTest.php \Drupal\Tests\bricks\Functional\BricksTest::testBricks()
  2. 8 tests/src/Kernel/BricksTest.php \Drupal\Tests\bricks\Kernel\BricksTest::testBricks()
Same name and namespace in other branches
  1. 2.x tests/src/Kernel/BricksTest.php \Drupal\Tests\bricks\Kernel\BricksTest::testBricks()

@dataProvider getTrees

File

tests/src/Kernel/BricksTest.php, line 51

Class

BricksTest
Class BricksTest

Namespace

Drupal\Tests\bricks\Kernel

Code

public function testBricks($tree) {
  $paragraphs = [];
  $strings = [];
  for ($i = 0; $i <= max(array_keys($tree)); $i++) {

    // Fighting escape rules of both QueryPath and PhpUnit is not fun and is
    // not a goal of this test so $this->>randomString() is not used.
    $string = $this
      ->randomMachineName();
    $paragraph = Paragraph::create([
      'type' => 'test',
      'testplain' => $string,
      'test' => array_intersect_key($paragraphs, array_flip($tree[$i] ?? [])),
    ]);
    $paragraph
      ->save();
    $paragraphs[$i] = $paragraph;
    $strings[] = $string;
  }
  $node = Node::create([
    'type' => 'test',
    'title' => 'test',
    'test' => array_intersect_key($paragraphs, $tree),
  ]);
  $node
    ->save();
  $build = \Drupal::entityTypeManager()
    ->getViewBuilder('node')
    ->view($node);
  $contents = (string) \Drupal::service('renderer')
    ->renderPlain($build);
  $keys = array_keys($tree);

  /** @var \QueryPath\DOMQuery $qp */
  $qp = QueryPath::withHTML5($contents);

  /** @var \QueryPath\DOMQuery $paragraphElement*/
  foreach ($qp
    ->firstChild()
    ->lastChild()
    ->firstChild()
    ->lastChild()
    ->lastChild()
    ->children() as $paragraphElement) {
    $key = array_shift($keys);
    $this
      ->assertTrue($paragraphElement
      ->hasClass('brick--id--' . $paragraphs[$key]
      ->id()));
    $this
      ->assertSame($strings[$key], $paragraphElement
      ->lastChild()
      ->lastChild()
      ->lastChild()
      ->text());
    unset($strings[$key]);

    /** @var \QueryPath\DOMQuery $childElement */
    foreach ($paragraphElement
      ->firstChild()
      ->children()
      ->children()
      ->children('.paragraph') as $childElement) {
      $childKey = array_shift($tree[$key]);
      $this
        ->assertSame($strings[$childKey], $childElement
        ->lastChild()
        ->lastChild()
        ->lastChild()
        ->text());
      unset($strings[$childKey]);
    }
  }
  $this
    ->assertEmpty($keys);
  $this
    ->assertEmpty(array_filter($tree));
  $this
    ->assertEmpty($strings);
}