You are here

class RealisticDummyContentDatabaseTestCase in Realistic Dummy Content 8.2

Same name and namespace in other branches
  1. 7.2 api/drupal7-simpletests/RealisticDummyContentDatabaseTestCase.test \RealisticDummyContentDatabaseTestCase

The test case

Hierarchy

Expanded class hierarchy of RealisticDummyContentDatabaseTestCase

File

api/drupal7-simpletests/RealisticDummyContentDatabaseTestCase.test, line 17
Drupal 7 tests.

View source
class RealisticDummyContentDatabaseTestCase extends DrupalWebTestCase {

  /**
   * Info for this test case.
   */
  public static function getInfo() {
    return array(
      'name' => t('Realistic dummy content functional test'),
      'description' => t('Test Realistic dummy content with a temporary database.'),
      'group' => 'Realistic dummy content',
    );
  }

  /**
   * Enable the module
   */
  public function setUp() {
    parent::setUp('realistic_dummy_content_api', 'realistic_dummy_content', 'devel_generate');

    // Our test needs to check that a given content was replaced, so turn
    // off random number generator during the test.
    variable_set('realistic_dummy_content_api_rand', REALISTIC_DUMMY_CONTENT_API_SEQUENTIAL);
  }

  /*
   * Test that attributes work correctly
   */
  public function testAttributes() {
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
    ));
    $modifier = new RealisticDummyContentFieldModifier($node, 'node');
    $attributes = $modifier
      ->GetAttributes();
    $existing = array();
    foreach ($attributes as $index => $attribute) {
      $name = $attribute
        ->GetName();
      $this
        ->assertFalse(in_array($name, $existing), 'Attribute ' . $name . ' only has one modifier');
      $existing[] = $name;
    }
  }

  /*
   * Test case for creating a node.
   */
  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');
  }

  /**
   * Assert that a node has a specified tag in its field_tags
   *
   * This will make sure the tag exists as the taxonomy term, and that it is
   * referenced in $node's field_tags
   *
   * @param string $tag_name
   *   A string
   * @param $node
   *   A node object
   */
  public function assertTag($tag_name, $node) {
    $term = taxonomy_get_term_by_name($tag_name);
    $this
      ->assertTrue($term, 'Term ' . $tag_name . ' exists');
    $referenced_term = taxonomy_term_load($node->field_tags[LANGUAGE_NONE][0]['tid']);
    if (isset($referenced_term->name)) {
      $referenced_term = $referenced_term->name;
    }
    else {
      $referenced_term = '[unknown term]';
    }
    $this
      ->assertTrue(array_key_exists($node->field_tags[LANGUAGE_NONE][0]['tid'], $term), 'Term is referenced in node ' . $node->nid . ' (' . $referenced_term . ' == ' . $tag_name . ')');
  }

  /*
   * Test case for creating a user.
   */
  public function testUser() {

    // Create a user with devel_generate
    module_load_include('inc', 'devel_generate');
    devel_create_users(1, 0);

    // Load the user and view
    $user = user_load(2);
    $filename = $user->picture->filename;
    $this
      ->assertTrue(drupal_substr($filename, 0, drupal_strlen('dummyfile')) == 'dummyfile', 'The user\'s picture file was replaced as expected. We know this because the filename starts with the string "dummyfile"');
    $current_picture = $user->picture;
    user_save($user);
    $user = user_load($user->uid);
    $this
      ->assertTrue($current_picture == $user->picture, 'The dummy file generation happens when the user is first created, not when it is resaved.');
  }

  /*
   * Test case for recipes.
   */
  public function testRecipe() {
    $this
      ->assertTrue(module_load_include('inc', 'realistic_dummy_content_api', 'realistic_dummy_content_api.drush'), 'drush file exists');
    realistic_dummy_content_api_apply_recipe(new RealisticDummyContentDebugLog());
    $page = node_load(4);
    $article = node_load(14);
    $this
      ->assertTrue(isset($page->type) && $page->type == 'page', 'Node 4 is a page, as specified in the recipe.');
    $this
      ->assertTrue(isset($article->type) && $article->type == 'article', 'Node 14 is an article, as specified in the recipe.');
  }

}

Members