You are here

public function EntityViewModeSuggestionsTest::testEntityTemplateSuggestions in Entity view modes 7

File

./entity_view_mode.test, line 230
Test integration for the entity_view_mode module.

Class

EntityViewModeSuggestionsTest

Code

public function testEntityTemplateSuggestions() {
  $node = $this
    ->drupalCreateNode();
  $build = node_view($node, 'node_test');
  $actual_suggestions = theme($build['#theme'], $build);
  $expected_suggestions = array(
    'node__' . $node->nid . '__node_test',
    'node__' . $node->nid,
    'node__' . $node->type . '__node_test',
    'node__' . $node->type,
  );
  $this
    ->assertIdentical($actual_suggestions, $expected_suggestions);

  // Rendering comments uses '#theme' => 'comment__node_type'.
  $comment = (object) array(
    'cid' => NULL,
    'nid' => $node->nid,
    'pid' => 0,
    'uid' => 0,
    'status' => COMMENT_PUBLISHED,
    'subject' => $this
      ->randomName(),
    'language' => LANGUAGE_NONE,
    'comment_body' => array(
      LANGUAGE_NONE => array(
        $this
          ->randomName(),
      ),
    ),
  );
  comment_save($comment);
  $build = comment_view($comment, $node, 'comment_test');
  $actual_suggestions = theme($build['#theme'], $build);
  $expected_suggestions = array(
    'comment__' . $comment->cid . '__comment_test',
    'comment__' . $comment->cid,
    'comment__' . 'comment_node_' . $node->type . '__comment_test',
    'comment__' . 'comment_node_' . $node->type,
  );
  $this
    ->assertIdentical($actual_suggestions, $expected_suggestions);

  // Users have no bundles.
  $account = $this->admin_user;
  $build = user_view($this->admin_user, 'user_test');
  $actual_suggestions = theme($build['#theme'], $build);
  $expected_suggestions = array(
    'user__' . $account->uid . '__user_test',
    'user__' . $account->uid,
  );
  $this
    ->assertIdentical($actual_suggestions, $expected_suggestions);
  $term = (object) array(
    'name' => $this
      ->randomName(),
    'vocabulary_machine_name' => 'tags',
  );
  taxonomy_term_save($term);
  $build = taxonomy_term_view($term, 'term_test');
  $actual_suggestions = theme($build['#theme'], $build);
  $expected_suggestions = array(
    'taxonomy_term__' . $term->tid . '__term_test',
    'taxonomy_term__' . $term->tid,
    'taxonomy_term__' . $term->vocabulary_machine_name . '__term_test',
    'taxonomy_term__' . $term->vocabulary_machine_name,
  );
  $this
    ->assertIdentical($actual_suggestions, $expected_suggestions);

  // Test the proper ordering of suggestions using the custom testing
  // theme function.
  $build = array(
    '#theme' => 'entity_view_mode_test',
    '#entity_view_mode' => array(
      'entity_type' => 'test',
      'id' => 'id',
      'bundle' => 'bundle',
      'view_mode' => 'view_mode',
      'langcode' => 'xx-test',
      'has_bundles' => TRUE,
    ),
  );
  $actual_suggestions = theme($build['#theme'], $build);
  $expected_suggestions = array(
    'test__first',
    'test__id__view_mode',
    'test__id',
    'test__bundle__view_mode',
    'test__bundle',
    'test__last',
  );
  $this
    ->assertIdentical($actual_suggestions, $expected_suggestions);
}