1<?php
2
3namespace Drupal\Tests\rest\Functional\Views;
4
5use Drupal\node\Entity\Node;
6use Drupal\Tests\views\Functional\ViewTestBase;
7use Drupal\views\Tests\ViewTestData;
8use Drupal\views\Views;
9
10/**
11 * Tests the display of an excluded field that is used as a token.
12 *
13 * @group rest
14 * @see \Drupal\rest\Plugin\views\display\RestExport
15 * @see \Drupal\rest\Plugin\views\row\DataFieldRow
16 */
17class ExcludedFieldTokenTest extends ViewTestBase {
18
19  /**
20   * @var \Drupal\views\ViewExecutable
21   */
22  protected $view;
23
24  /**
25   * The views that are used by this test.
26   *
27   * @var array
28   */
29  public static $testViews = ['test_excluded_field_token_display'];
30
31  /**
32   * {@inheritdoc}
33   */
34  protected $defaultTheme = 'stark';
35
36  /**
37   * The modules that need to be installed for this test.
38   *
39   * @var array
40   */
41  protected static $modules = [
42    'entity_test',
43    'rest_test_views',
44    'node',
45    'field',
46  ];
47
48  /**
49   * {@inheritdoc}
50   */
51  protected function setUp($import_test_views = TRUE): void {
52    parent::setUp($import_test_views);
53
54    ViewTestData::createTestViews(static::class, ['rest_test_views']);
55
56    // Create some test content.
57    for ($i = 1; $i <= 10; $i++) {
58      Node::create([
59        'type' => 'article',
60        'title' => 'Article test ' . $i,
61      ])->save();
62    }
63
64    $this->enableViewsTestModule();
65
66    $this->view = Views::getView('test_excluded_field_token_display');
67    $this->view->setDisplay('rest_export_1');
68  }
69
70  /**
71   * Tests the display of an excluded title field when used as a token.
72   */
73  public function testExcludedTitleTokenDisplay() {
74    $actual_json = $this->drupalGet($this->view->getPath(), ['query' => ['_format' => 'json']]);
75    $this->assertSession()->statusCodeEquals(200);
76
77    $expected = [
78      ['nothing' => 'Article test 10'],
79      ['nothing' => 'Article test 9'],
80      ['nothing' => 'Article test 8'],
81      ['nothing' => 'Article test 7'],
82      ['nothing' => 'Article test 6'],
83      ['nothing' => 'Article test 5'],
84      ['nothing' => 'Article test 4'],
85      ['nothing' => 'Article test 3'],
86      ['nothing' => 'Article test 2'],
87      ['nothing' => 'Article test 1'],
88    ];
89    $this->assertSame(json_encode($expected), $actual_json);
90  }
91
92}
93