You are here

public function ShareMessageWorkflowTest::testShareMessageWorkflow in Share Message 8

Main Share Message workflow through the admin UI.

File

tests/src/Functional/ShareMessageWorkflowTest.php, line 22

Class

ShareMessageWorkflowTest
Main Share Message workflow through the admin UI.

Namespace

Drupal\Tests\sharemessage\Functional

Code

public function testShareMessageWorkflow() {

  // Step 1: Create a Share Message in the UI.
  $this
    ->drupalGet('admin/config/services/sharemessage/add');
  $edit = [
    'label' => 'Share Message Test Label',
    'id' => 'sharemessage_test_label',
    'title' => 'Share Message Test Title',
    'message_long' => 'Share Message Test Long Description',
    'message_short' => 'Share Message Test Short Description',
    'image_url' => 'http://www.example.com/drupal.jpg',
    'share_url' => 'http://www.example.com',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(t('Share Message @label has been added.', [
    '@label' => $edit['label'],
  ]), 'Share Message is successfully saved.');

  // Step 2: Display Share Message and verify AddThis markup
  // and meta header elements.
  $this
    ->drupalGet('sharemessage-test/sharemessage_test_label');
  $this
    ->assertShareButtons($edit, 'addthis_16x16_style', TRUE);
  $this
    ->assertOGTags('og:title', $edit['title']);
  $this
    ->assertOGTags('og:description', $edit['message_long']);
  $this
    ->assertOGTags('og:image', $edit['image_url']);
  $this
    ->assertOGTags('og:url', $edit['share_url']);
  $this
    ->drupalGet('admin/config/services/sharemessage/add');

  // Check if the enforce checkbox is there.
  $this
    ->assertFieldByName('enforce_usage', NULL, 'The enforce checkbox was found.');
  $edit_2 = [
    'label' => 'Share Message 2 Test Label',
    'id' => 'sharemessage_test_label2',
    'title' => 'Share Message 2 Test Title',
    'message_long' => 'Share Message 2 Test Long Description',
    'message_short' => 'Share Message 2 Test Short Description',
    'image_url' => $edit['image_url'],
    'share_url' => $edit['share_url'],
    'enforce_usage' => 1,
  ];
  $this
    ->drupalPostForm(NULL, $edit_2, t('Save'));

  /** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
  $sharemessage = \Drupal::entityTypeManager()
    ->getStorage('sharemessage')
    ->load('sharemessage_test_label2');

  // Check if the option was saved as expected.
  $this
    ->assertEqual($sharemessage->enforce_usage, TRUE, 'Enforce setting was saved on the entity.');
  $this
    ->drupalGet('sharemessage-test/sharemessage_test_label', [
    'query' => [
      'smid' => 'sharemessage_test_label2',
    ],
  ]);

  // Check if the og:description tag gets rendered correctly.
  $this
    ->assertOGTags('og:description', $edit_2['message_long']);

  // Check if the og:url tag gets rendered correctly.
  $url = Url::fromUri($edit['share_url'], [
    'query' => [
      'smid' => 'sharemessage_test_label2',
    ],
  ])
    ->toString();
  $this
    ->assertOGTags('og:url', $url);
  $message_description = 'Suppressing og:url meta tag for overridden sharemessage.';
  $this
    ->assertNoOGTags('og:url', $edit['share_url'], $message_description);

  // Check if the overridden Share Message is rendered correctly.
  $this
    ->assertRaw('addthis:description="' . $edit['message_long'] . '"', 'Overridden sharemessage has og data as attributes.');

  // Disable enforcement of overrides in the global settings.
  $this
    ->config('sharemessage.settings')
    ->set('message_enforcement', FALSE)
    ->save();
  $this
    ->drupalGet('sharemessage-test/sharemessage_test_label', [
    'query' => [
      'smid' => 'sharemessage_test_label2',
    ],
  ]);

  // Check if the og:description tag no longer displays the override.
  $this
    ->assertOGTags('og:description', $edit['message_long']);

  // Check if the og:url tag no longer displays the override.
  $this
    ->assertOGTags('og:url', $edit['share_url']);
}