You are here

protected function CropFunctionalTest::doTestFileUriAlter in Crop API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/CropFunctionalTest.php \Drupal\Tests\crop\Functional\CropFunctionalTest::doTestFileUriAlter()

Asserts a shortened hash is added to the file URI.

Tests crop_file_url_alter().

1 call to CropFunctionalTest::doTestFileUriAlter()
CropFunctionalTest::testCropTypeCrud in tests/src/Functional/CropFunctionalTest.php
Tests crop type crud pages.

File

tests/src/Functional/CropFunctionalTest.php, line 157

Class

CropFunctionalTest
Functional tests for crop API.

Namespace

Drupal\Tests\crop\Functional

Code

protected function doTestFileUriAlter() {

  // Get the test file.
  file_unmanaged_copy(drupal_get_path('module', 'crop') . '/tests/files/sarajevo.png', PublicStream::basePath());
  $file_uri = 'public://sarajevo.png';
  $file = File::create([
    'uri' => $file_uri,
    'status' => FILE_STATUS_PERMANENT,
  ]);
  $file
    ->save();

  /** @var \Drupal\crop\CropInterface $crop */
  $values = [
    'type' => $this->cropType
      ->id(),
    'entity_id' => $file
      ->id(),
    'entity_type' => $file
      ->getEntityTypeId(),
    'uri' => 'public://sarajevo.png',
    'x' => '100',
    'y' => '150',
    'width' => '200',
    'height' => '250',
  ];
  $crop = Crop::create($values);
  $crop
    ->save();

  // Test that the hash is appended both when a URL is created and passed
  // through file_create_url() and when a URL is created, without additional
  // file_create_url() calls.
  $shortened_hash = substr(md5(implode($crop
    ->position()) . implode($crop
    ->anchor())), 0, 8);

  // Build an image style derivative for the file URI.
  $image_style_uri = $this->testStyle
    ->buildUri($file_uri);
  $image_style_uri_url = file_create_url($image_style_uri);
  $this
    ->assertTrue(strpos($image_style_uri_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');

  // Build an image style URL.
  $image_style_url = $this->testStyle
    ->buildUrl($file_uri);
  $this
    ->assertTrue(strpos($image_style_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');

  // Update the crop to assert the hash has changed.
  $crop
    ->setPosition('80', '80')
    ->save();
  $old_hash = $shortened_hash;
  $new_hash = substr(md5(implode($crop
    ->position()) . implode($crop
    ->anchor())), 0, 8);
  $image_style_url = $this->testStyle
    ->buildUrl($file_uri);
  $this
    ->assertFalse(strpos($image_style_url, $old_hash) !== FALSE, 'The image style URL does not contain the old hash.');
  $this
    ->assertTrue(strpos($image_style_url, $new_hash) !== FALSE, 'The image style URL contains an updated hash.');

  // Delete the file and the crop entity associated,
  // the crop entity are auto cleaned by crop_file_delete().
  $file
    ->delete();

  // Check that the crop entity is correctly deleted.
  $this
    ->assertFalse(Crop::cropExists($file_uri), 'The Crop entity was correctly deleted after file delete.');
}