RowEntityTest.php 2.41 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\views\Kernel\Plugin;
4

5
use Drupal\Core\Form\FormState;
6
7
use Drupal\entity_test\Entity\EntityTest;
use Drupal\user\Entity\User;
8
use Drupal\views\Views;
9
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10
11
12
13

/**
 * Tests the generic entity row plugin.
 *
14
 * @group views
15
16
 * @see \Drupal\views\Plugin\views\row\EntityRow
 */
17
class RowEntityTest extends ViewsKernelTestBase {
18
19
20
21
22
23

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = ['entity_test', 'field', 'system', 'user'];
25
26
27
28
29
30
31
32
33
34
35

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_entity_row');

  /**
   * {@inheritdoc}
   */
36
  protected function setUp($import_test_views = TRUE) {
37
    parent::setUp($import_test_views);
38

39
40
    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('user');
41
42
43
44
45
46
  }

  /**
   * Tests the entity row handler.
   */
  public function testEntityRow() {
47
48
49
50
    $user = User::create([
      'name' => 'test user',
    ]);
    $user->save();
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    $entity_test = EntityTest::create([
      'user_id' => $user->id(),
      'name' => 'test entity test',
    ]);
    $entity_test->save();

    // Ensure entities have different ids.
    if ($entity_test->id() == $user->id()) {
      $entity_test->delete();
      $entity_test = EntityTest::create([
        'user_id' => $user->id(),
        'name' => 'test entity test',
      ]);
      $entity_test->save();
    }

    $view = Views::getView('test_entity_row');
    $build = $view->preview();
    $this->render($build);

    $this->assertText('test entity test');
    $this->assertNoText('Member for');

    // Change the view to use a relationship to render the row.
76
    $view = Views::getView('test_entity_row');
77
78
79
80
    $display = &$view->storage->getDisplay('default');
    $display['display_options']['row']['type'] = 'entity:user';
    $display['display_options']['row']['options']['relationship'] = 'user_id';
    $view->setDisplay('default');
81
82
    $build = $view->preview();
    $this->render($build);
83

84
85
    $this->assertNoText('test entity test');
    $this->assertText('Member for');
86
87
88

    // Tests the available view mode options.
    $form = array();
89
    $form_state = new FormState();
90
    $form_state->set('view', $view->storage);
91
92
93
    $view->rowPlugin->buildOptionsForm($form, $form_state);

    $this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available');
94
95
96
  }

}