You are here

public function ConsumerImageSylesFunctionalTest::testRead in Consumer Image Styles 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()
  2. 8.2 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()
  3. 4.x tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()

Test the GET method.

File

tests/src/Functional/ConsumerImageSylesFunctionalTest.php, line 122

Class

ConsumerImageSylesFunctionalTest
@group consumer_image_styles

Namespace

Drupal\Tests\consumer_image_styles\Functional

Code

public function testRead() {
  $this
    ->createDefaultContent(1);

  // 1. Check the request for the image directly.
  $path = sprintf('/jsonapi/file/file/%s', $this->files[0]
    ->uuid());
  $query = [
    '_consumer_id' => $this->consumer
      ->uuid(),
  ];
  $raw = $this
    ->drupalGet($path, [
    'query' => $query,
  ]);
  $output = Json::decode($raw);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $derivatives = $output['data']['meta']['derivatives'];
  $this
    ->assertContains('/files/styles/foo/public/', $derivatives['foo']);
  $this
    ->assertContains('/files/styles/bar/public/', $derivatives['bar']);
  $this
    ->assertContains('itok=', $derivatives['foo']);
  $this
    ->assertContains('itok=', $derivatives['bar']);

  // 2. Check the request via the node.
  $path = sprintf('/jsonapi/node/%s/%s', $this->contentType
    ->id(), $this->nodes[0]
    ->uuid());
  $query = [
    '_consumer_id' => $this->consumer
      ->uuid(),
    'include' => $this->imageFieldName,
  ];
  $raw = $this
    ->drupalGet($path, [
    'query' => $query,
  ]);
  $output = Json::decode($raw);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $derivatives = $output['included'][0]['meta']['derivatives'];
  $this
    ->assertContains(file_create_url('public://styles/foo/public/'), $derivatives['foo']);
  $this
    ->assertContains(file_create_url('public://styles/bar/public/'), $derivatives['bar']);
  $this
    ->assertContains('itok=', $derivatives['foo']);
  $this
    ->assertContains('itok=', $derivatives['bar']);

  // 3. Check the request for the image directly without consumer.
  $path = sprintf('/jsonapi/file/file/%s', $this->files[0]
    ->uuid());
  $raw = $this
    ->drupalGet($path);
  $output = Json::decode($raw);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertTrue(empty($output['data']['meta']['derivatives']));
}