EntityViewControllerTest.php 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Entity\EntityViewControllerTest.
 */

namespace Drupal\system\Tests\Entity;

10
use Drupal\entity_test\Entity\EntityTest;
11 12 13
use Drupal\simpletest\WebTestBase;

/**
14 15 16
 * Tests EntityViewController functionality.
 *
 * @group Entity
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */
class EntityViewControllerTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity_test');

  /**
   * Array of test entities.
   *
   * @var array
   */
  protected $entities = array();

34
  protected function setUp() {
35
    parent::setUp();
36
    // Create some dummy entity_test entities.
37
    for ($i = 0; $i < 2; $i++) {
38
      $entity_test = $this->createTestEntity('entity_test');
39 40 41 42
      $entity_test->save();
      $this->entities[] = $entity_test;
    }

43
    $this->drupalLogin($this->drupalCreateUser(['view test entity']));
44 45 46 47 48 49
  }

  /**
   * Tests EntityViewController.
   */
  function testEntityViewController() {
50
    $get_label_markup = function($label) {
51
      return '<h1 class="page-title">
52 53
            <div class="field field--name-name field--type-string field--label-hidden field__item">' . $label . '</div>
      </h1>';
54 55
    };

56
    foreach ($this->entities as $entity) {
57
      $this->drupalGet('entity_test/' . $entity->id());
58
      $this->assertRaw($entity->label());
59
      $this->assertRaw($get_label_markup($entity->label()));
60 61
      $this->assertRaw('full');

62
      $this->drupalGet('entity_test_converter/' . $entity->id());
63 64 65
      $this->assertRaw($entity->label());
      $this->assertRaw('full');

66
      $this->drupalGet('entity_test_no_view_mode/' . $entity->id());
67 68 69
      $this->assertRaw($entity->label());
      $this->assertRaw('full');
    }
70 71 72 73 74

    // As entity_test IDs must be integers, make sure requests for non-integer
    // IDs return a page not found error.
    $this->drupalGet('entity_test/invalid');
    $this->assertResponse(404);
75
  }
76 77 78 79 80

  /**
   * Tests field item attributes.
   */
  public function testFieldItemAttributes() {
81 82
    // Make sure the test field will be rendered.
    entity_get_display('entity_test', 'entity_test', 'default')
83 84 85 86
      ->setComponent('field_test_text', array('type' => 'text_default'))
      ->save();

    // Create an entity and save test value in field_test_text.
87
    $test_value = $this->randomMachineName();
88
    $entity = EntityTest::create();
89 90 91 92 93
    $entity->field_test_text = $test_value;
    $entity->save();

    // Browse to the entity and verify that the attribute is rendered in the
    // field item HTML markup.
94
    $this->drupalGet('entity_test/' . $entity->id());
95
    $xpath = $this->xpath('//div[@data-field-item-attr="foobar"]/p[text()=:value]', array(':value' => $test_value));
96 97 98 99
    $this->assertTrue($xpath, 'The field item attribute has been found in the rendered output of the field.');

    // Enable the RDF module to ensure that two modules can add attributes to
    // the same field item.
100
    \Drupal::service('module_installer')->install(array('rdf'));
101 102
    $this->resetAll();

103 104
    // Set an RDF mapping for the field_test_text field. This RDF mapping will
    // be turned into RDFa attributes in the field item output.
105
    $mapping = rdf_get_mapping('entity_test', 'entity_test');
106 107 108 109 110
    $mapping->setFieldMapping('field_test_text', array(
      'properties' => array('schema:text'),
    ))->save();
    // Browse to the entity and verify that the attributes from both modules
    // are rendered in the field item HTML markup.
111
    $this->drupalGet('entity_test/' . $entity->id());
112
    $xpath = $this->xpath('//div[@data-field-item-attr="foobar" and @property="schema:text"]/p[text()=:value]', array(':value' => $test_value));
113 114 115
    $this->assertTrue($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
  }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  /**
   * Tests that a view builder can successfully override the view builder.
   */
  public function testEntityViewControllerViewBuilder() {
    $entity_test = $this->createTestEntity('entity_test_view_builder');
    $entity_test->save();
    $this->drupalGet('entity_test_view_builder/' . $entity_test->id());
    $this->assertText($entity_test->label());
  }

  /**
   * Creates an entity for testing.
   *
   * @param string $entity_type
   *   The entity type.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The created entity.
   */
  protected function createTestEntity($entity_type) {
    $data = array(
      'bundle' => $entity_type,
      'name' => $this->randomMachineName(),
    );
    return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);
  }

143
}