You are here

public function AmpFormatterMediaImageTest::createAmpNode in Accelerated Mobile Pages (AMP) 8.3

Create a node with desired content for testing.

Overrides AmpTestBase::createAmpNode

File

tests/src/Functional/AmpFormatterMediaImageTest.php, line 77

Class

AmpFormatterMediaImageTest
Tests AMP Media Image formatter.

Namespace

Drupal\Tests\amp\Functional

Code

public function createAmpNode() {

  // Create a node with a media image to test AMP media formatter.
  parent::createAmpNode();
  $images = $this
    ->getTestFiles('image');
  $image = array_pop($images);
  $file = File::create([
    'uri' => $image->uri,
  ]);
  $file
    ->save();

  // Create media.
  $media_field_name = $this
    ->config('media.type.image')
    ->get('source_configuration.source_field');
  $media = Media::create([
    'bundle' => 'image',
    'name' => $file
      ->id(),
    $media_field_name => $file,
  ]);
  $media
    ->save();

  // Create a new node with reference to media.
  $this->node
    ->set($this->fieldName, [
    'target_id' => $media
      ->id(),
  ])
    ->save();

  // Test subparts of markup to avoid failures due to line breaks.
  $file_path = $file
    ->createFileUrl();
  $this->valuesOut = [
    '<amp-img layout="responsive" width="16" height="9" src="' . $file_path . '">',
    '</amp-img>',
  ];
}