class StyleSerializerTest

Same name in this branch
  1. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest
Same name and namespace in other branches
  1. 11.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest
  2. 10 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest

Tests the serializer style plugin.

@group hal @group legacy

Hierarchy

Expanded class hierarchy of StyleSerializerTest

See also

\Drupal\rest\Plugin\views\display\RestExport

\Drupal\rest\Plugin\views\style\Serializer

\Drupal\rest\Plugin\views\row\DataEntityRow

\Drupal\rest\Plugin\views\row\DataFieldRow

File

core/modules/hal/tests/src/Functional/rest/Views/StyleSerializerTest.php, line 21

Namespace

Drupal\Tests\hal\Functional\rest\Views
View source
class StyleSerializerTest extends ViewTestBase {
  use AssertPageCacheContextsAndTagsTrait;
  
  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = [
    'entity_test',
    'hal',
    'hal_test_views',
    'node',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = [
    'test_serializer_display_entity',
  ];
  
  /**
   * A user with administrative privileges to look at test entity and configure views.
   */
  protected $adminUser;
  
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE, $modules = [
    'hal_test_views',
  ]) : void {
    parent::setUp($import_test_views, $modules);
    $this->adminUser = $this->drupalCreateUser([
      'administer entity_test content',
      'access user profiles',
      'view test entity',
    ]);
    // Save some entity_test entities.
    for ($i = 1; $i <= 10; $i++) {
      EntityTest::create([
        'name' => 'test_' . $i,
        'user_id' => $this->adminUser
          ->id(),
      ])
        ->save();
    }
    $this->enableViewsTestModule();
  }
  
  /**
   * Checks the behavior of the Serializer callback paths and row plugins.
   */
  public function testSerializerResponses() {
    // Test the entity rows.
    $view = Views::getView('test_serializer_display_entity');
    $view->initDisplay();
    $this->executeView($view);
    // Get the serializer service.
    $serializer = $this->container
      ->get('serializer');
    $entities = [];
    foreach ($view->result as $row) {
      $entities[] = $row->_entity;
    }
    $expected_cache_tags = $view->getCacheTags();
    $expected_cache_tags[] = 'entity_test_list';
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    foreach ($entities as $entity) {
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $entity->getCacheTags());
    }
    $expected = $serializer->serialize($entities, 'hal_json');
    $actual_json = $this->drupalGet('test/serialize/entity', [
      'query' => [
        '_format' => 'hal_json',
      ],
    ]);
    $this->assertSame($expected, $actual_json, 'The expected HAL output was found.');
    $this->assertCacheTags($expected_cache_tags);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.