aggregator-2.x-dev/tests/src/Functional/Jsonapi/FeedTest.php

tests/src/Functional/Jsonapi/FeedTest.php
<?php

namespace Drupal\Tests\aggregator\Functional\Jsonapi;

use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Url;
use Drupal\jsonapi\JsonApiSpec;
use Drupal\Tests\jsonapi\Functional\ResourceTestBase;
use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;

/**
 * JSON:API integration test for the "Feed" content entity type.
 *
 * @group aggregator
 */
class FeedTest extends ResourceTestBase {

  use CommonCollectionFilterAccessTestPatternsTrait;

  /**
   * A feed title.
   *
   * @see Drupal\Tests\aggregator\Functional\Jsonapi\FeedTest::getPostDocument()
   */
  const POST_DOCUMENT_TITLE = 'Feed Resource Post Test';

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['aggregator'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

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

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

  /**
   * {@inheritdoc}
   *
   * @var \Drupal\config_test\ConfigTestInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  protected static $patchProtectedFieldNames = [];

  /**
   * {@inheritdoc}
   */
  protected static $uniqueFieldNames = ['url'];

  /**
   * {@inheritdoc}
   */
  protected function setUpAuthorization($method) {
    switch ($method) {
      case 'GET':
        $this->grantPermissionsToTestedRole(['access news feeds']);
        break;

      case 'POST':
      case 'PATCH':
      case 'DELETE':
        $this->grantPermissionsToTestedRole(['administer news feeds']);
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createEntity() {
    $feed = Feed::create();
    $feed->set('fid', 1)
      ->setTitle('Feed')
      ->setUrl('http://example.com/rss.xml')
      ->setDescription('Feed Resource Test 1')
      ->setRefreshRate(900)
      ->setLastCheckedTime(123456789)
      ->setQueuedTime(123456789)
      ->setWebsiteUrl('http://example.com')
      ->setImage('http://example.com/feed_logo')
      ->setEtag('hijklmn')
      ->setLastModified(123456789)
      ->save();

    return $feed;
  }

  /**
   * {@inheritdoc}
   */
  protected function createAnotherEntity($key) {
    /** @var \Drupal\aggregator\FeedInterface $duplicate */
    $duplicate = $this->getEntityDuplicate($this->entity, $key);
    $duplicate->set('field_rest_test', 'Duplicate feed entity');
    $duplicate->setUrl("http://example.com/$key.xml");
    $duplicate->save();
    return $duplicate;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/aggregator_feed/aggregator_feed/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => JsonApiSpec::SUPPORTED_SPECIFICATION_PERMALINK],
          ],
        ],
        'version' => JsonApiSpec::SUPPORTED_SPECIFICATION_VERSION,
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
      'data' => [
        'id' => $this->entity->uuid(),
        'type' => 'aggregator_feed--aggregator_feed',
        'links' => [
          'self' => ['href' => $self_url],
        ],
        'attributes' => [
          'url' => 'http://example.com/rss.xml',
          'title' => 'Feed',
          'refresh' => 900,
          'checked' => '1973-11-29T21:33:09+00:00',
          'queued' => '1973-11-29T21:33:09+00:00',
          'link' => 'http://example.com',
          'description' => 'Feed Resource Test 1',
          'image' => 'http://example.com/feed_logo',
          'etag' => 'hijklmn',
          'modified' => '1973-11-29T21:33:09+00:00',
          'langcode' => 'en',
          'drupal_internal__fid' => 1,
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getPostDocument() {
    return [
      'data' => [
        'type' => 'aggregator_feed--aggregator_feed',
        'attributes' => [
          'title' => self::POST_DOCUMENT_TITLE,
          'url' => 'http://example.com/feed',
          'refresh' => 900,
          'description' => 'Feed Resource Post Test Description',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function doTestPatchIndividual(): void {
    // doTestPostIndividual() runs before this and creates a feed based on
    // getPostDocument(). Because of that, Aggregator's title and URL uniqueness
    // validation cause doTestPatchIndividual() to fail when it tries to edit
    // the feed to have that same title and URL. The feed created by
    // doTestPostIndividual() must first be deleted from the database.
    $ids = \Drupal::entityQuery('aggregator_feed')
      ->accessCheck(FALSE)
      ->condition('title', self::POST_DOCUMENT_TITLE)
      ->execute();
    $feeds = \Drupal::entityTypeManager()
      ->getStorage('aggregator_feed')
      ->loadMultiple($ids);
    foreach ($feeds as $feed) {
      $feed->delete();
    }

    parent::doTestPatchIndividual();
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedUnauthorizedAccessMessage($method) {
    return match($method) {
      'GET' => "The 'access news feeds' permission is required.",
      default => "The 'administer news feeds' permission is required.",
    };
  }

  /**
   * {@inheritdoc}
   */
  public function testCollectionFilterAccess() {
    $this->doTestCollectionFilterAccessBasedOnPermissions('title', 'access news feeds');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc