You are here

public function RealisticDummyContentDbTestCase::testNode in Realistic Dummy Content 8

File

api/src/Tests/RealisticDummyContentDbTestCase.php, line 46
This file contains the testing code which requires the database. These tests are slower than the unit tests so we want to limit them.

Class

RealisticDummyContentDbTestCase
Test Realistic dummy content with a temporary database.

Namespace

Drupal\realistic_dummy_content_api\Tests

Code

public function testNode() {

  // Create a node with the devel_generate property set.
  $nids = array();
  for ($i = 1; $i <= 9; $i++) {
    $node_values = array(
      'title' => $this
        ->randomString(),
      'type' => 'article',
      'body' => array(
        \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED => array(
          array(
            $this
              ->randomString(),
          ),
        ),
      ),
    );
    $node_values['devel_generate'] = array(
      'whatever',
    );
    $node = entity_create('node', $node_values);
    $node
      ->save();
    $nids[$node
      ->id()] = $node
      ->id();
  }

  // Load the nodes
  $nodes = Node::loadMultiple($nids);
  $expected_values = array(
    'title' => array(
      'F',
      'S',
      'T',
    ),
    'body' => array(
      'D',
      'I',
      NULL,
    ),
    'tag' => array(
      'people',
      'historical',
      'events',
    ),
  );
  $images_with_alt = array();
  foreach ($nodes as $nid => $node) {

    // The node should have replaced the image with our own.
    $image_set = isset($node->field_image[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED]);
    $this
      ->assertTrue($image_set, 'Node image is set. There exists an image for this node because $node->field_image[\\Drupal\\Core\\Language\\Language::LANGCODE_NOT_SPECIFIED] is not empty.');
    if (!$image_set) {
      continue;
    }
    $filename = $node->field_image[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['filename'];
    $this
      ->assertTrue(Unicode::substr($filename, 0, Unicode::strlen('dummyfile')) == 'dummyfile', 'The image file was replaced as expected for node/article/field_image. We know this because the filename starts with the string "dummyfile"');
    if (isset($node->field_image[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['alt'])) {
      $images_with_alt[$node
        ->id()] = $node->field_image[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['alt'];
    }
    $title = $node->title[0];
    $title_expected = $expected_values['title'][($nid - 1) % 3];
    $body_expected = $expected_values['body'][($nid - 1) % count($expected_values['body'])];
    if ($body_expected == 'I') {
      $body_format = $node->body[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['format'];
      $this
        ->assertTrue($body_format == 'full_html', 'Using full html for node ' . $nid . ' as expected (using ' . $body_format . ').');
    }
    elseif ($body_expected) {
      $this
        ->assertTrue($node->body[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['format'] == 'filtered_html', 'Using filtered html for node ' . $nid . ' as expected.');
    }
    $this
      ->assertTrue($title == $title_expected, 'Title first letter (' . $title . ') is as expected (' . $title_expected . ') for this nid (' . $nid . ')');
    if ($body_expected === NULL) {
      $this
        ->assertTrue($node->body == array(), 'Body is not set because we have an empty file.');
    }
    else {
      $this
        ->assertTrue($node->body[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['value'][0] == $body_expected, 'Body first letter (' . $node->body[\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED][0]['value'][0] . ') is as expected (' . $body_expected . ') for this nid (' . $nid . ')');
    }
    $this
      ->assertTag($expected_values['tag'][($nid - 1) % 3], $node);
  }
  $this
    ->assertTrue(count($images_with_alt) == 3, 'Three images are expected to have an alt because even though several images exist, we are using the sequential method and therefore only cycling through the first three (because there are only three values to other fields); ' . count($images_with_alt) . ' have an alt text; ' . (count($nodes) - count($images_with_alt)) . ' do not have an alt text');
}