acquia_dam-1.0.0-rc1/tests/src/FunctionalJavascript/AcquiaDamWebDriverTestBase.php
tests/src/FunctionalJavascript/AcquiaDamWebDriverTestBase.php
<?php declare(strict_types=1); namespace Drupal\Tests\acquia_dam\FunctionalJavascript; use Drupal\editor\Entity\Editor; use Drupal\filter\Entity\FilterFormat; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\user\UserInterface; use PHPUnit\Runner\BaseTestRunner; /** * Testing base class for all WebDriver tests in acquia_dam. */ abstract class AcquiaDamWebDriverTestBase extends WebDriverTestBase { use EntityReferenceTestTrait; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected static $modules = [ 'block', 'node', 'ckeditor', 'field_ui', 'acquia_dam', 'acquia_dam_test', // Install dblog to assist with debugging. 'dblog', ]; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->placeBlock('local_tasks_block'); $this->placeBlock('local_actions_block'); $this->placeBlock('page_title_block'); FilterFormat::create([ 'format' => 'test_format', 'name' => 'Test format', 'filters' => [ 'media_embed' => ['status' => TRUE], 'filter_html' => [ 'status' => TRUE, 'settings' => [ // Indirectly tests `data-embed-code-id` is added to filter_html. // @todo needs a kernel test for this as well to directly test. 'allowed_html' => '<drupal-media data-entity-type data-entity-uuid data-view-mode data-align data-caption alt title>', ], ], ], ])->save(); // If a test is using CKEditor5, configure that editor instead of CKEditor4. if (property_exists(static::class, 'modules') && in_array('ckeditor5', static::$modules, TRUE)) { $editor = 'ckeditor5'; $editor_settings = [ 'toolbar' => [ 'items' => [ 'drupalMedia', 'sourceEditing', ], ], 'plugins' => [ 'ckeditor5_sourceEditing' => [ 'allowed_tags' => [], ], 'media_media' => [ 'allow_view_mode_override' => TRUE, ], ], ]; } else { $editor = 'ckeditor'; $editor_settings = [ 'toolbar' => [ 'rows' => [ [ [ 'name' => 'Main', 'items' => [ 'Source', 'Undo', 'Redo', ], ], ], [ [ 'name' => 'Embeds', 'items' => [ 'DrupalMediaLibrary', ], ], ], ], ], ]; } Editor::create([ 'editor' => $editor, 'format' => 'test_format', 'settings' => $editor_settings, ])->save(); $this->drupalCreateContentType(['type' => 'page']); // Suppressing itok to make the image style URLs easier to test. $config = $this->config('image.settings'); $config->set('suppress_itok_output', TRUE)->save(); } /** * {@inheritdoc} */ protected function tearDown(): void { $status = $this->getStatus(); if ($status === BaseTestRunner::STATUS_ERROR || $status === BaseTestRunner::STATUS_WARNING || $status === BaseTestRunner::STATUS_FAILURE) { $log = \Drupal::database() ->select('watchdog', 'w') ->fields('w') ->execute() ->fetchAll(); throw new \RuntimeException(var_export($log, TRUE)); } parent::tearDown(); } /** * Grants the current user a dummy DAM token. * * @param string $token * Token of the user object. */ protected function grantCurrentUserDamToken(string $token = NULL): void { if (!$token) { $token = $this->randomString(); } self::assertInstanceOf(UserInterface::class, $this->loggedInUser); $this->container->get('acquia_dam.authentication_service')->setUserData( (int) $this->loggedInUser->id(), [ 'acquia_dam_username' => $this->loggedInUser->getEmail(), 'acquia_dam_token' => $token, ] ); } /** * Grants the site a dummy DAM domain. */ protected function grantDamDomain() { /** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */ $configFactory = $this->container->get('config.factory'); $configFactory->getEditable('acquia_dam.settings') ->set('domain', 'test.widencollective.com') ->save(); } /** * Grants the site a dummy DAM token and domain configuration. */ protected function grantSiteRegistrationToken() { $this->grantDamDomain(); $this->container->get('state')->set( 'acquia_dam_token', $this->randomString() ); } /** * Select an asset and import it from the media library. * * @param string $asset_id * The asset ID. * @param string $asset_type * The asset type. * @param string $format * Display format of the asset. * @param bool $is_editor * True if the call coming from a ck_editor, False if it's a field widget. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function selectAndInsertAsset(string $asset_id, string $asset_type, string $format = '', bool $is_editor = TRUE): void { $modal = $this->assertSession()->waitForElement('css', '#drupal-modal'); $modal->clickLink($asset_type); $this->assertSession()->assertWaitOnAjaxRequest(); $media_select_checkbox = $this->assertSession()->elementExists('css', "[value='$asset_id']", $modal); $media_select_checkbox->check(); if ($is_editor) { $this->pressDialogButton('Next: Select Format'); $this->assertSession()->assertWaitOnAjaxRequest(); $this->assertSession()->pageTextContains('Choose a format for your media'); $this->getSession()->getPage()->selectFieldOption('embed_code', $format); $this->assertSession()->assertWaitOnAjaxRequest(); } $this->pressDialogButton('Insert selected'); } /** * Presses a button in the dialog. * * Drupal hides the original buttons and places them in a special area of * the dialog. This ensures the proper button is clicked and assertions do not * error on clicking a hidden button. * * @param string $locator * The button locator. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function pressDialogButton(string $locator): void { $this->assertSession()->elementExists('css', '.ui-dialog-buttonpane')->pressButton($locator); $this->assertSession()->assertWaitOnAjaxRequest(); } /** * Creating a media field for the test. */ protected function createMediaReferenceField($cardinality = -1) { $this->createEntityReferenceField('node', 'page', 'media_field', 'A Media Field', 'media', 'default', [], $cardinality); $display_repository = $this->container->get('entity_display.repository'); $display_repository->getFormDisplay('node', 'page') ->setComponent('media_field', [ 'type' => 'media_library_widget', 'region' => 'content', ]) ->save(); $display_repository->getViewDisplay('node', 'page', 'default') ->setComponent('media_field', [ 'type' => 'entity_reference_entity_view', ]) ->save(); } /** * Helper function to log in a user with necessary permission and access. * * @throws \Drupal\Core\Entity\EntityStorageException. */ protected function createAndLoginContentCreator() { $user = $this->drupalCreateUser([ 'use text format test_format', 'access media overview', 'create page content', 'edit any page content', ]); $this->drupalLogin($user); } }