You are here

public function EntityTest::testNodeView in Image Replace 8

Same name and namespace in other branches
  1. 7 src/Tests/EntityTest.php \Drupal\image_replace\Tests\EntityTest::testNodeView()

Tests image replacement on node entities.

File

src/Tests/EntityTest.php, line 73

Class

EntityTest
Tests core entity API integration for the replace image effect.

Namespace

Drupal\image_replace\Tests

Code

public function testNodeView() {
  list($original_file, $replacement_file) = $this
    ->createTestFiles();
  $file_system = \Drupal::service('file_system');
  $node = Node::create([
    'type' => 'article',
    'title' => $this
      ->randomString(16),
    'promote' => 1,
  ]);
  $node->image_original->target_id = $original_file
    ->id();
  $node->image_original->alt = $alt = $this
    ->randomMachineName();
  $node->image_original->title = $title = $this
    ->randomMachineName();
  $node->image_replacement->target_id = $replacement_file
    ->id();
  $node->image_replacement->alt = $alt = $this
    ->randomMachineName();
  $node->image_replacement->title = $title = $this
    ->randomMachineName();
  $node
    ->save();

  // Check teaser.
  $this
    ->drupalGet('node');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $generated_url = ImageStyle::load($this->styleName)
    ->buildUrl($node->image_original->entity
    ->getFileUri());
  $relative_url = file_url_transform_relative($generated_url);
  $this
    ->assertSession()
    ->responseContains(Html::escape($relative_url));
  $generated_image_data = $this
    ->drupalGet($generated_url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $default_scheme = \Drupal::config('system.file')
    ->get('default_scheme');
  $file_destination = $default_scheme . '://';

  // Assert that the result is the replacement image.
  $generated_uri = $file_system
    ->saveData($generated_image_data, $file_destination);
  $this
    ->assertTrue($this
    ->imageIsReplacement($generated_uri), 'The generated file should be the same as the replacement file on teaser.');

  // Check full view.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $generated_url = file_create_url($node->image_original->entity
    ->getFileUri());
  $relative_url = file_url_transform_relative($generated_url);
  $this
    ->assertSession()
    ->responseContains(Html::escape($relative_url));
  $generated_image_data = $this
    ->drupalGet($generated_url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Assert that the result is the original image.
  $generated_uri = $file_system
    ->saveData($generated_image_data, $file_destination);
  $this
    ->assertTrue($this
    ->imageIsOriginal($generated_uri), 'The generated file should be the same as the original file on full node view.');
}