Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-4838: Allow to switch from local/remote to circabc. #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AVPortalQuery extends QueryPluginBase {
*
* phpcs:disable Drupal.Semantics.FunctionTriggerError.TriggerErrorTextLayoutRelaxed
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AvPortalClientFactory $client_factory, ConfigFactoryInterface $config_factory, ModuleExtensionList $moduleExtensionList = NULL) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, AvPortalClientFactory $client_factory, ConfigFactoryInterface $config_factory, ?ModuleExtensionList $moduleExtensionList = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->clientFactory = $client_factory;
$this->config = $config_factory->get('media_avportal.settings');
Expand Down Expand Up @@ -122,7 +122,7 @@ public static function create(ContainerInterface $container, array $configuratio
* @return string
* Table alias name.
*/
public function ensureTable(string $table, string $relationship = NULL): string {
public function ensureTable(string $table, ?string $relationship = NULL): string {
return '';
}

Expand All @@ -144,7 +144,7 @@ public function ensureTable(string $table, string $relationship = NULL): string
* @return string
* The field name.
*/
public function addField(string $table, string $field = NULL, string $alias = NULL, array $params = []): string {
public function addField(string $table, ?string $field = NULL, ?string $alias = NULL, array $params = []): string {
return $field;
}

Expand Down Expand Up @@ -249,7 +249,7 @@ protected function createViewResults(array $results, ViewExecutable $view): void
* @param string $operator
* The condition operator.
*/
public function addWhere(int $group = 0, string $field = NULL, $value = NULL, string $operator = NULL): void {
public function addWhere(int $group = 0, ?string $field = NULL, $value = NULL, ?string $operator = NULL): void {
if (empty($group)) {
$group = 0;
}
Expand Down
12 changes: 12 additions & 0 deletions modules/oe_media_circabc/oe_media_circabc.module
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ function oe_media_circabc_media_presave(MediaInterface $media) {
$media->set('langcode', _oe_media_circabc_get_drupal_langcode($document->getLangcode()));
}

// Clear the remote or local field values in case the user switched from a
// local or remote media to CircaBC.
foreach ($media->getTranslationLanguages(TRUE) as $language) {
$translation = $media->getTranslation($language->getId());
if (!$translation->get('oe_media_file')->isEmpty()) {
$translation->set('oe_media_file', []);
}
if (!$translation->get('oe_media_remote_file')->isEmpty()) {
$translation->set('oe_media_remote_file', []);
}
}

\Drupal::service('oe_media_circabc.document_mapper')->mapDocumentMedia($document, $media);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/oe_media_circabc/src/CircaBc/CircaBcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function fillTranslations(CircaBcDocument $document): void {
/**
* {@inheritdoc}
*/
public function query(string $uuid, string $langcode = NULL, string $query_string = NULL, int $page = 1, int $limit = 10): CircaBcDocumentResult {
public function query(string $uuid, ?string $langcode = NULL, ?string $query_string = NULL, int $page = 1, int $limit = 10): CircaBcDocumentResult {
$endpoint = $this->config['url'] . '/service/circabc/files';
$query = [
'node' => $uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function fillTranslations(CircaBcDocument $document): void;
* @return \Drupal\oe_media_circabc\CircaBc\CircaBcDocumentResult
* The results.
*/
public function query(string $uuid, string $langcode = NULL, string $query_string = NULL, int $page = 1, int $limit = 10): CircaBcDocumentResult;
public function query(string $uuid, ?string $langcode = NULL, ?string $query_string = NULL, int $page = 1, int $limit = 10): CircaBcDocumentResult;

/**
* Loads the available interest groups.
Expand Down
6 changes: 4 additions & 2 deletions modules/oe_media_circabc/src/DocumentMediaFormHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ protected function addCircaBcFormElement(array &$form, FormStateInterface $form_
'select[name="' . $name . '"]' => ['value' => 'circabc'],
],
],
// We can only reference a document when creating media.
'#access' => $media->isNew(),
// We can only reference a document when creating media or when the
// existing media is not of the type CircaBC (so we allow users to
// switch from local or remote to CircaBC).
'#access' => $media->isNew() || $media->get('oe_media_file_type')->value !== 'circabc',
// In case we are in a IEF context in which we don't yet save the media
// before reopening the edit form.
'#default_value' => $form_state->get('oe_media_circa_bc_ief_url') ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function create(ContainerInterface $container, array $configuratio
* @return string
* Table alias name.
*/
public function ensureTable(string $table, string $relationship = NULL): string {
public function ensureTable(string $table, ?string $relationship = NULL): string {
return '';
}

Expand All @@ -114,7 +114,7 @@ public function ensureTable(string $table, string $relationship = NULL): string
* @return string
* The field name.
*/
public function addField(string $table, string $field = NULL, string $alias = NULL, array $params = []): string {
public function addField(string $table, ?string $field = NULL, ?string $alias = NULL, array $params = []): string {
return $field;
}

Expand Down Expand Up @@ -217,7 +217,7 @@ protected function createViewResults(CircaBcDocumentResult $result, ViewExecutab
* @param string|null $operator
* The condition operator.
*/
public function addWhere(int $group = 0, string $field = NULL, $value = NULL, string $operator = NULL): void {
public function addWhere(int $group = 0, ?string $field = NULL, $value = NULL, ?string $operator = NULL): void {
if (empty($group)) {
$group = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\oe_media\Traits\MediaTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Entity\View;

/**
Expand Down Expand Up @@ -346,6 +347,75 @@ public function testCircaBcEntityBrowserWidget(): void {
$this->assertSingleMediaEntity($media_title);
}

/**
* Tests that we can switch from local or remote to CircaBC.
*/
public function testSwitchToCircaBc(): void {
$this->container->get('file_system')->copy(
\Drupal::service('extension.list.module')
->getPath('oe_media') . '/tests/fixtures/sample.pdf',
'public://sample.pdf'
);

$this->container->get('file_system')->copy(
\Drupal::service('extension.list.module')
->getPath('oe_media') . '/tests/fixtures/sample.pdf',
'public://sample_2.pdf'
);

$file_one = File::create([
'uri' => 'public://sample.pdf',
]);
$file_one->save();

$file_two = File::create([
'uri' => 'public://sample_2.pdf',
]);
$file_two->save();

$media_storage = $this->container->get('entity_type.manager')->getStorage('media');

/** @var \Drupal\media\MediaInterface $media */
$local_media = $media_storage->create([
'name' => 'a document media',
'bundle' => 'document',
'oe_media_file_type' => 'local',
'oe_media_file' => $file_one,
]);
$local_media->addTranslation('fr', ['oe_media_file' => $file_two] + $local_media->toArray());
$local_media->save();

$this->drupalGet($local_media->toUrl('edit-form'));
$this->getSession()->getPage()->selectFieldOption('File Type', 'CircaBC');
$this->getSession()->getPage()->fillField('CircaBC URL', 'https://example.com/circabc-ewpp/ui/group/85a095a8-aacb-4ae2-9f67-c90a789e353e/library/e74e3bc0-a639-4e04-a839-3bbd60ed5688/details');
$this->getSession()->getPage()->pressButton('Save');
$media_storage->resetCache();
$local_media = $media_storage->load($local_media->id());
$this->assertEquals('circabc', $local_media->get('oe_media_file_type')->value);
$this->assertTrue($local_media->get('oe_media_file')->isEmpty());
$this->assertTrue($local_media->getTranslation('fr')->get('oe_media_file')->isEmpty());

/** @var \Drupal\media\MediaInterface $media */
$remote_media = $media_storage->create([
'name' => 'a document media',
'bundle' => 'document',
'oe_media_file_type' => 'remote',
'oe_media_remote_file' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
]);
$remote_media->addTranslation('fr', ['oe_media_remote_file' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy-fr.pdf'] + $remote_media->toArray());
$remote_media->save();

$this->drupalGet($remote_media->toUrl('edit-form'));
$this->getSession()->getPage()->selectFieldOption('File Type', 'CircaBC');
$this->getSession()->getPage()->fillField('CircaBC URL', 'https://example.com/circabc-ewpp/ui/group/85a095a8-aacb-4ae2-9f67-c90a789e353e/library/e74e3bc0-a639-4e04-a839-3bbd60ed5688/details');
$this->getSession()->getPage()->pressButton('Save');
$media_storage->resetCache();
$remote_media = $media_storage->load($remote_media->id());
$this->assertEquals('circabc', $remote_media->get('oe_media_file_type')->value);
$this->assertTrue($remote_media->get('oe_media_remote_file')->isEmpty());
$this->assertTrue($remote_media->getTranslation('fr')->get('oe_media_remote_file')->isEmpty());
}

/**
* Asserts that only a single Media entity with the given title was created.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Drupal\Tests\oe_media_circabc\Kernel;

use Drupal\Core\Site\Settings;
use Drupal\Tests\oe_media\Kernel\MediaTestBase;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\oe_media_circabc\Plugin\views\query\CircaBcQuery;
use Drupal\Tests\oe_media\Kernel\MediaTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function preSave(ContentEntityInterface $entity): void {
/**
* {@inheritdoc}
*/
public function getLinks(int $limit = NULL, int $offset = 0): LinkCollectionInterface {
public function getLinks(?int $limit = NULL, int $offset = 0): LinkCollectionInterface {
$links = new LinkCollection();
$ids = $this->configuration['media'];
if (!$ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Behat\Mink\Element\NodeElement;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\media\Entity\Media;
use Drupal\Tests\oe_link_lists\Traits\LinkListTestTrait;
use Drupal\media\Entity\Media;

/**
* Tests the Gallery link list bundle.
Expand Down
2 changes: 1 addition & 1 deletion modules/oe_media_gallery/tests/src/Kernel/GalleryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Drupa\Tests\oe_media_gallery\Kernel;

use Drupal\Tests\oe_media\Kernel\MediaTestBase;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\oe_link_lists\Entity\LinkList;
use Drupal\oe_link_lists\EntityAwareLinkInterface;
use Drupal\Tests\oe_media\Kernel\MediaTestBase;

/**
* Tests the Gallery link list type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Drupal\Tests\oe_media_iframe\Kernel;

use Drupal\Tests\media\Kernel\MediaKernelTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\media\Entity\MediaType;
use Drupal\Tests\media\Kernel\MediaKernelTestBase;

/**
* Tests iframe media source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\oe_media_iframe\Unit;

use Drupal\oe_media_iframe\Plugin\Filter\FilterIframeTag;
use Drupal\Tests\UnitTestCase;
use Drupal\oe_media_iframe\Plugin\Filter\FilterIframeTag;

/**
* Tests the filter_iframe_tag filter plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Drupal\Tests\oe_media_link_lists\FunctionalJavascript;

use Drupal\Tests\oe_link_lists_manual_source\FunctionalJavascript\ManualLinkListTestBase;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\oe_link_lists\DefaultLink;
use Drupal\Tests\oe_link_lists_manual_source\FunctionalJavascript\ManualLinkListTestBase;

/**
* Tests the manual internal media links.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\oe_media_webtools\Kernel;

use Drupal\oe_media_webtools\Plugin\Field\FieldWidget\OpPublicationListIdWidget;
use Drupal\Tests\oe_media\Kernel\MediaTestBase;
use Drupal\oe_media_webtools\Plugin\Field\FieldWidget\OpPublicationListIdWidget;

/**
* Tests the 'OP Publication List ID' field widget.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class OEmbedClientMiddleware {
* @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList
* The module extension list.
*/
public function __construct(ConfigFactoryInterface $configFactory, EventDispatcherInterface $eventDispatcher, ModuleExtensionList $moduleExtensionList = NULL) {
public function __construct(ConfigFactoryInterface $configFactory, EventDispatcherInterface $eventDispatcher, ?ModuleExtensionList $moduleExtensionList = NULL) {
$this->config = $configFactory->get('media.settings');
$this->eventDispatcher = $eventDispatcher;

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Behat/MediaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Context\ConfigContext;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Drupal\file\FileInterface;
use Drupal\Tests\oe_media\Traits\MediaTestTrait;
use Drupal\file\FileInterface;

/**
* Context to related to media testing.
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Kernel/MediaAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Drupal\Tests\oe_media\Kernel;

use Drupal\media\Entity\Media;
use Drupal\Tests\media\Kernel\MediaKernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\media\Entity\Media;
use Drupal\user\Entity\Role;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Kernel/MediaImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Drupal\Tests\oe_media\Kernel;

use Drupal\Tests\media\Kernel\MediaKernelTestBase;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Kernel\MediaKernelTestBase;

/**
* Tests image media.
Expand Down