You are here

public function realistic_dummy_content_DatabaseTestCase::testNode in Realistic Dummy Content 7

File

api/tests/realistic_dummy_content_api.db.test, line 53
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

realistic_dummy_content_DatabaseTestCase
The test case

Code

public function testNode() {

  // Create a node with the devel_generate property set.
  $nids = array();
  for ($i = 1; $i <= 9; $i++) {
    $node = (object) array(
      'title' => $this
        ->randomName(),
      'type' => 'article',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(),
          ),
        ),
      ),
    );
    $node->devel_generate = array(
      'whatever',
    );
    node_save($node);
    $nids[$node->nid] = $node->nid;
  }

  // Load the nodes
  $nodes = node_load_multiple($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[LANGUAGE_NONE]);
    $this
      ->assertTrue($image_set, 'Node image is set. There exists an image for this node because $node->field_image[LANGUAGE_NONE] is not empty.');
    if (!$image_set) {
      continue;
    }
    $filename = $node->field_image[LANGUAGE_NONE][0]['filename'];
    $this
      ->assertTrue(drupal_substr($filename, 0, drupal_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[LANGUAGE_NONE][0]['alt'])) {
      $images_with_alt[$node->nid] = $node->field_image[LANGUAGE_NONE][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[LANGUAGE_NONE][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[LANGUAGE_NONE][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[LANGUAGE_NONE][0]['value'][0] == $body_expected, 'Body first letter (' . $node->body[LANGUAGE_NONE][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');
}