You are here

public function RabbitHolePageRedirectActionTest::testTokenizedUrlRedirect in Rabbit Hole 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/RabbitHolePageRedirectActionTest.php \Drupal\Tests\rabbit_hole\Functional\RabbitHolePageRedirectActionTest::testTokenizedUrlRedirect()

Test URL redirect with token value.

File

tests/src/Functional/RabbitHolePageRedirectActionTest.php, line 91

Class

RabbitHolePageRedirectActionTest
Test the "Page redirect" action.

Namespace

Drupal\Tests\rabbit_hole\Functional

Code

public function testTokenizedUrlRedirect() {

  // Test redirect with default system token.
  $node = $this
    ->createTestNode('page_redirect');
  $node
    ->set('rh_redirect', '[site:url]');
  $node
    ->set('rh_redirect_response', 301);
  $node
    ->save();
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $expected_url = Url::fromRoute('<front>');
  $this
    ->assertSession()
    ->addressEquals($expected_url);

  // Test more complex scenarios with nested entities.
  // Attach media field to Article content type.
  $storage = FieldStorageConfig::create([
    'entity_type' => 'node',
    'field_name' => 'field_related_media',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'field_storage' => $storage,
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Related media',
    'settings' => [
      'handler_settings' => [
        'target_bundles' => [
          'document' => 'document',
        ],
      ],
    ],
  ])
    ->save();
  $file = $this
    ->createTestFile('first');
  $media = Media::create([
    'bundle' => 'document',
    'name' => $this
      ->randomString(),
    'field_media_document' => $file
      ->id(),
  ]);
  $media
    ->save();
  $node = Node::create([
    'title' => $this
      ->randomString(),
    'type' => 'article',
    'field_related_media' => [
      'target_id' => $media
        ->id(),
    ],
    'rh_action' => 'page_redirect',
    'rh_redirect' => '[node:field_related_media:entity:field_media_document:entity:url]',
    'rh_redirect_response' => 301,
  ]);
  $node
    ->save();
  $this
    ->drupalGet($node
    ->toUrl());
  $expected_url = file_create_url($file
    ->getFileUri());
  $this
    ->assertSession()
    ->addressEquals($expected_url);
  $this
    ->assertSession()
    ->responseContains('first');

  // Change the file in media entity and verify that destination changed.
  $file2 = $this
    ->createTestFile('second file');
  $media
    ->set('field_media_document', $file2
    ->id());
  $media
    ->save();
  $this
    ->drupalGet($node
    ->toUrl());
  $expected_url = file_create_url($file2
    ->getFileUri());
  $this
    ->assertSession()
    ->addressEquals($expected_url);
  $this
    ->assertSession()
    ->responseContains('second');
}