acquia_dam-1.0.0-rc1/tests/src/FunctionalJavascript/CKEditor4IntegrationTest.php

tests/src/FunctionalJavascript/CKEditor4IntegrationTest.php
<?php

namespace Drupal\Tests\acquia_dam\FunctionalJavascript;

use Drupal\acquia_dam\Entity\MediaExpiryDateField;
use Drupal\acquia_dam\Entity\MediaSourceField;
use Drupal\Core\Template\Attribute;
use Drupal\media\Entity\Media;
use Drupal\Tests\ckeditor\Traits\CKEditorTestTrait;

/**
 * CKEditor4 integration tests.
 *
 * @group acquia_dam
 * @requires module ckeditor
 */
final class CKEditor4IntegrationTest extends AcquiaDamWebDriverTestBase {

  use CKEditorTestTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'starterkit_theme';

  /**
   * Tests the "Update media" button.
   */
  public function testUpdateMediaButton(): void {
    $this->createAndLoginContentCreator();
    $this->grantSiteRegistrationToken();
    $this->grantCurrentUserDamToken();
    // Only images have version support, but the only fixture with multiple
    // versions is the video.
    $media = Media::create([
      'bundle' => 'acquia_dam_video_asset',
      MediaSourceField::SOURCE_FIELD_NAME => [
        'asset_id' => 'efb03f75-3c42-497b-baa9-5ec79d1f56af',
        'version_id' => '6d8ff607-723a-474f-992a-38833a57f97b',
        'external_id' => '8a1ouvfchk',
      ],
    ]);
    $media->save();
    $first_revision = $media->getRevisionId();
    $media->setNewRevision();
    $media->set(MediaSourceField::SOURCE_FIELD_NAME, [
      'asset_id' => 'efb03f75-3c42-497b-baa9-5ec79d1f56af',
      'version_id' => '04984534-8682-4fbf-95ae-f3c7b46af9ee',
      'external_id' => '8a1ouvfchk',
    ]);
    $media->save();
    $second_revision = $media->getRevisionId();
    self::assertNotEquals($first_revision, $second_revision);

    $this->drupalGet('/node/add/page');
    $this->getSession()->getPage()->fillField('Title', 'Embed test');
    $this->waitForEditor();

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original')
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertSession()->assertWaitOnAjaxRequest();
      $this->assertSession()->buttonExists('Edit media');
      $this->assertSession()->buttonNotExists('Update media');
    });

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $first_revision)
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertSession()->assertWaitOnAjaxRequest();
      $this->assertWaitForButtonExist('Edit media');
      $this->assertWaitForButtonExist('Update media');
    });

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $second_revision)
    );
    $this->assertInEditor(function () {
      self::assertNotNull(
        $this->assertSession()->waitForElementVisible('css', 'drupal-media', 2000)
      );
      $this->assertWaitForButtonExist('Edit media');
      $this->assertSession()->buttonNotExists('Update media');
    });

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $first_revision)
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertWaitForButtonExist('Update media');
      $this->getSession()->getPage()->pressButton('Update media');
      $this->getSession()->switchToIFrame();
      $this->assertSession()->waitForElement('css', '#drupal-modal');
      $this->assertSession()->pageTextContains('You are about to update your current media to a newer version.');
      $this->assertSession()->pageTextContains('Current media');
      $this->assertSession()->pageTextContains('Updated media');
      $this->pressDialogButton('Cancel');
      $this->getSession()->switchToIFrame('ckeditor');
    });
    $this->assertEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $first_revision)
    );

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $first_revision)
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertWaitForButtonExist('Update media');
      $this->getSession()->getPage()->pressButton('Update media');
      $this->getSession()->switchToIFrame();
      $this->assertSession()->waitForElement('css', '#drupal-modal');
      $this->assertSession()->pageTextContains('You are about to update your current media to a newer version.');
      $this->assertSession()->pageTextContains('Current media');
      $this->assertSession()->pageTextContains('Updated media');
      $this->pressDialogButton('Update');
      $this->getSession()->switchToIFrame('ckeditor');
    });
    $this->assertEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original', $second_revision)
    );
  }

  /**
   * Tests the expiry tag on CKEdtior.
   */
  public function testExpiryTag() {
    $this->createAndLoginContentCreator();
    $this->grantSiteRegistrationToken();
    $this->grantCurrentUserDamToken();
    $this->createMediaReferenceField();
    $yesterday = \Drupal::time()->getCurrentTime() - 86400;
    $tomorrow = \Drupal::time()->getCurrentTime() + 86400;
    $media_expired = Media::create([
      'bundle' => 'acquia_dam_video_asset',
      MediaSourceField::SOURCE_FIELD_NAME => [
        'asset_id' => 'efb03f75-3c42-497b-baa9-5ec79d1f56af',
      ],
      MediaExpiryDateField::EXPIRY_DATE_FIELD_NAME => [
        'value' => $yesterday,
      ],
    ]);

    $media_expired->save();

    $media = Media::create([
      'bundle' => 'acquia_dam_pdf_asset',
      MediaSourceField::SOURCE_FIELD_NAME => [
        'asset_id' => '0324b0b2-5293-4aa0-b0aa-c85b003395e2',
      ],
      MediaExpiryDateField::EXPIRY_DATE_FIELD_NAME => [
        'value' => $tomorrow,
      ],
    ]);
    $media->save();

    $this->drupalGet('/node/add/page');
    $this->getSession()->getPage()->fillField('Title', 'Embed test');
    $this->waitForEditor();

    $this->setEditorContent(
      $this->getDrupalMediaString($media_expired->uuid(), 'original')
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertSession()->assertWaitOnAjaxRequest();
      $this->assertSession()->buttonExists('Edit media');
      $this->assertSession()->waitForText('Expired media');
    });

    $this->setEditorContent(
      $this->getDrupalMediaString($media->uuid(), 'original')
    );
    $this->assertInEditor(function () {
      $this->assertWaitForDrupalMediaVisible();
      $this->assertSession()->assertWaitOnAjaxRequest();
      $this->assertSession()->buttonExists('Edit media');
      $this->assertSession()->pageTextNotContains('Expired media');
    });
  }

  /**
   * Asserts that a button exists after waiting.
   *
   * @param string $locator
   *   The locator.
   */
  private function assertWaitForButtonExist(string $locator): void {
    self::assertNotNull(
      $this->assertSession()->waitForButton($locator)
    );
  }

  /**
   * Asserts that the <drupal-media> element is visible after waiting.
   */
  private function assertWaitForDrupalMediaVisible(): void {
    self::assertNotNull(
      $this->assertSession()->waitForElementVisible('css', 'drupal-media', 2000)
    );
  }

  /**
   * Perform assertions inside the CKEditor editor iframe.
   *
   * @param callable $callable
   *   The callable, to perform assertions.
   */
  private function assertInEditor(callable $callable): void {
    $this->assignNameToCkeditorIframe();
    $this->getSession()->switchToIFrame('ckeditor');
    $callable();
    $this->getSession()->switchToIFrame();
  }

  /**
   * Asserts the CKEditor content directly.
   *
   * @param string $expected
   *   The expected content.
   */
  private function assertEditorContent(string $expected): void {
    $this->pressEditorButton('source');
    $content = $this->assertSession()
      ->waitForElement('css', 'textarea.cke_source')
      ->getValue();
    self::assertSame(trim($expected), trim($content));
    $this->pressEditorButton('source');
  }

  /**
   * Sets the CKEditor content directly.
   *
   * @param string $content
   *   The content.
   */
  private function setEditorContent(string $content): void {
    $this->pressEditorButton('source');
    $this->assertSession()
      ->waitForElement('css', 'textarea.cke_source')
      ->setValue($content);
    $this->pressEditorButton('source');
  }

  /**
   * Gets the string for a <drupal-media> element.
   *
   * @param string $uuid
   *   The media entity UUID.
   * @param string $embed_code
   *   The embed code.
   * @param string $revision_id
   *   Optional: the revision ID for the media entity.
   *
   * @return string
   *   The <drupal-media> element string.
   */
  private function getDrupalMediaString(string $uuid, string $embed_code, string $revision_id = ''): string {
    // CKEditor sorts attributes alphabetically, so we also build our string
    // the same way to make testing easier.
    $attributes = new Attribute([
      'data-embed-code-id' => $embed_code,
      'data-entity-revision' => $revision_id,
      'data-entity-type' => 'media',
      'data-entity-uuid' => $uuid,
      'data-view-mode' => '',
    ]);
    return "<drupal-media$attributes></drupal-media>";
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc