You are here

SearchPageTest.php in JSON:API 8

Same filename and directory in other branches
  1. 8.2 tests/src/Functional/SearchPageTest.php

File

tests/src/Functional/SearchPageTest.php
View source
<?php

namespace Drupal\Tests\jsonapi\Functional;

use Drupal\Core\Url;
use Drupal\search\Entity\SearchPage;

/**
 * JSON API integration test for the "SearchPage" config entity type.
 *
 * @group jsonapi
 */
class SearchPageTest extends ResourceTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'node',
    'search',
  ];

  /**
   * {@inheritdoc}
   */
  protected static $entityTypeId = 'search_page';

  /**
   * {@inheritdoc}
   */
  protected static $resourceTypeName = 'search_page--search_page';

  /**
   * {@inheritdoc}
   *
   * @var \Drupal\search\SearchPageInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  protected function setUpAuthorization($method) {
    switch ($method) {
      case 'GET':
        $this
          ->grantPermissionsToTestedRole([
          'access content',
        ]);
        break;
      case 'POST':
      case 'PATCH':
      case 'DELETE':
        $this
          ->grantPermissionsToTestedRole([
          'administer search',
        ]);
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
    $search_page = SearchPage::create([
      'id' => 'hinode_search',
      'plugin' => 'node_search',
      'label' => 'Search of magnetic activity of the Sun',
      'path' => 'sun',
    ]);
    $search_page
      ->save();
    return $search_page;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/search_page/search_page/' . $this->entity
      ->uuid())
      ->setAbsolute()
      ->toString(TRUE)
      ->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => 'http://jsonapi.org/format/1.0/',
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => $self_url,
      ],
      'data' => [
        'id' => $this->entity
          ->uuid(),
        'type' => 'search_page--search_page',
        'links' => [
          'self' => $self_url,
        ],
        'attributes' => [
          'configuration' => [
            'rankings' => [],
          ],
          'dependencies' => [
            'module' => [
              'node',
            ],
          ],
          'id' => 'hinode_search',
          'label' => 'Search of magnetic activity of the Sun',
          'langcode' => 'en',
          'path' => 'sun',
          'plugin' => 'node_search',
          'status' => TRUE,
          'uuid' => $this->entity
            ->uuid(),
          'weight' => 0,
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getPostDocument() {

    // @todo Update in https://www.drupal.org/node/2300677.
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedUnauthorizedAccessMessage($method) {
    switch ($method) {
      case 'GET':
        return "The 'access content' permission is required.";
      default:
        return parent::getExpectedUnauthorizedAccessMessage($method);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedUnauthorizedAccessCacheability() {

    // @see \Drupal\search\SearchPageAccessControlHandler::checkAccess()
    return parent::getExpectedUnauthorizedAccessCacheability()
      ->addCacheTags([
      'config:search.page.hinode_search',
    ]);
  }

}

Classes

Namesort descending Description
SearchPageTest JSON API integration test for the "SearchPage" config entity type.