diff --git a/entity_reference_preview.module b/entity_reference_preview.module index 68f51a7f36eafc034e12a76ad620a902fb5ee4b7..92dc31f96b97032207604f1ff50ec21b80937a75 100644 --- a/entity_reference_preview.module +++ b/entity_reference_preview.module @@ -13,6 +13,7 @@ use Drupal\entity_reference_preview\Entity\EntityStateManager; use Drupal\entity_reference_preview\PreviewDetectorPluginManager; use Drupal\entity_reference_preview\PreviewDetectorToolbar; use Drupal\views\Plugin\views\row\EntityRow; +use Drupal\views\Plugin\views\row\Fields; use Drupal\views\ResultRow; use Drupal\views\ViewExecutable; diff --git a/src/Entity/EntityStateManager.php b/src/Entity/EntityStateManager.php index 35c09ff1acb8e32ec77eefe6912c595e4a47cc8c..930e62f6ae44f5caab054f64f5b127253909dac7 100644 --- a/src/Entity/EntityStateManager.php +++ b/src/Entity/EntityStateManager.php @@ -107,6 +107,7 @@ class EntityStateManager { $swapped_entities = $entity_storage->loadMultipleRevisions($revision_ids); return array_reduce($swapped_entities, function (array $carry, RevisionableInterface $entity) use ($entities, $account) { $entity_id = $entity->id(); + /** @var \Drupal\Core\Entity\RevisionableInterface $old_entity */ $old_entity = current( array_filter($entities, function (EntityInterface $e) use ($entity_id) { return $e->id() === $entity_id; diff --git a/src/Events/PreviewNegotiationSubscriber.php b/src/Events/PreviewNegotiationSubscriber.php index 767e0071816d9cc80cea36ad0c60cfddee5ed1cc..3a1110160ec3cc0dccaa7c600241060134ee22bf 100644 --- a/src/Events/PreviewNegotiationSubscriber.php +++ b/src/Events/PreviewNegotiationSubscriber.php @@ -4,7 +4,7 @@ namespace Drupal\entity_reference_preview\Events; use Drupal\entity_reference_preview\PreviewDetectorPluginManager; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; /** @@ -48,10 +48,10 @@ class PreviewNegotiationSubscriber implements EventSubscriberInterface { /** * Sets a flag in the request object if it belongs to a "latest" route. * - * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event * The event. */ - public function setPreview(GetResponseEvent $event): void { + public function setPreview(RequestEvent $event): void { $request = $event->getRequest(); // Set the global request state in the request object for others to read. $active_detector = $this->previewDetector->activeDetector($request); diff --git a/src/Form/PreviewActionsForm.php b/src/Form/PreviewActionsForm.php index 1fdf1a1a309aa195fc9f2371c29fa940fdb9b2ce..a8987a402b17f67acb6790e02f5047786fa626cc 100644 --- a/src/Form/PreviewActionsForm.php +++ b/src/Form/PreviewActionsForm.php @@ -30,7 +30,7 @@ class PreviewActionsForm extends FormBase { * @param \Drupal\entity_reference_preview\Plugin\PreviewDetector\CookiePreviewDetector $cookie_detector * The state detector plugin. */ - public function __construct(CookiePreviewDetector $cookie_detector) { + public function __construct(CookiePreviewDetector $cookie_detector, private PreviewDetectorPluginManager $previewDetectorPluginManager) { $this->cookieDetector = $cookie_detector; } @@ -38,7 +38,7 @@ class PreviewActionsForm extends FormBase { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - $cookie_detector = $container->get(PreviewDetectorPluginManager::class) + $cookie_detector = $this->previewDetectorPluginManager ->createInstance('cookie'); return new static($cookie_detector); } diff --git a/src/Plugin/Field/FieldFormatter/EntityReferenceEntityPreviewFormatter.php b/src/Plugin/Field/FieldFormatter/EntityReferenceEntityPreviewFormatter.php index 401dd1d4bf5621ee1196f8da7d0309be9ff1135d..ee91cf19763b253e34d0da66737a3a56139c8085 100644 --- a/src/Plugin/Field/FieldFormatter/EntityReferenceEntityPreviewFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EntityReferenceEntityPreviewFormatter.php @@ -83,7 +83,8 @@ class EntityReferenceEntityPreviewFormatter extends EntityReferenceEntityFormatt EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, EntityStateManager $entity_state_manager, - bool $can_see_indicator + bool $can_see_indicator, + EntityStateManager $entityStateManager ) { parent::__construct( $plugin_id, @@ -99,6 +100,7 @@ class EntityReferenceEntityPreviewFormatter extends EntityReferenceEntityFormatt ); $this->entityStateManager = $entity_state_manager; $this->canSeeIndicator = $can_see_indicator; + $this->entityStateManager = $entityStateManager; } /** @@ -121,7 +123,7 @@ class EntityReferenceEntityPreviewFormatter extends EntityReferenceEntityFormatt $container->get('logger.factory'), $container->get('entity_type.manager'), $container->get('entity_display.repository'), - $container->get(EntityStateManager::class), + $this->entityStateManager, $can_see_indicator && $indicator_enabled ); } @@ -155,7 +157,7 @@ class EntityReferenceEntityPreviewFormatter extends EntityReferenceEntityFormatt // "multiple entity load" to load all the entities for the multiple // "entity reference item lists" being displayed. We thus cannot use // \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities(). - $target_ids = []; + $target_ids = $revision_ids = []; $target_entities = []; $target_type = $this->getFieldSetting('target_type'); foreach ($entities_items as $items) { @@ -173,6 +175,7 @@ class EntityReferenceEntityPreviewFormatter extends EntityReferenceEntityFormatt } } if ($target_ids) { + /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */ $entity_storage = $this->entityTypeManager->getStorage($target_type); $revision_ids = $this->entityStateManager ->findRevisionIds($target_type, $target_ids); diff --git a/src/Plugin/PreviewDetector/RenderedEntityPreviewDetector.php b/src/Plugin/PreviewDetector/RenderedEntityPreviewDetector.php index 35393741dc8c6ac23e28480b47ddef0ec2a1878b..667cd5c3684941a55cc5b59f689ca66cceb26a49 100644 --- a/src/Plugin/PreviewDetector/RenderedEntityPreviewDetector.php +++ b/src/Plugin/PreviewDetector/RenderedEntityPreviewDetector.php @@ -76,7 +76,7 @@ class RenderedEntityPreviewDetector extends PreviewDetectorPluginBase { /** * The entity preview function. * - * @param \Drupal\Core\Entity\ContentEntityInterface|null $entity + * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The content entity to check. * * @return bool diff --git a/src/PreviewDetectorPluginManager.php b/src/PreviewDetectorPluginManager.php index 107e19ec7f3b51277fe11d3bdac6b27382c7815f..7784b071cb4a4b68ff91061435b4c4f7eccaf8e5 100644 --- a/src/PreviewDetectorPluginManager.php +++ b/src/PreviewDetectorPluginManager.php @@ -3,6 +3,7 @@ namespace Drupal\entity_reference_preview; use Drupal\Component\Plugin\FallbackPluginManagerInterface; +use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; @@ -76,6 +77,7 @@ class PreviewDetectorPluginManager extends DefaultPluginManager implements Fallb return array_reduce( $instances, static function ($detected, PreviewDetectorInterface $detector) use ($request) { + assert($detector instanceof PluginInspectionInterface); if ($detected) { return $detected; } diff --git a/tests/src/Functional/EntityReferencePreviewFunctionalTestBase.php b/tests/src/Functional/EntityReferencePreviewFunctionalTestBase.php index 60af8164157ce5c4b29e8d47701a3c4f0c993d80..37a42e69656e1fae4e7427eb1654fd3ba60be2c9 100644 --- a/tests/src/Functional/EntityReferencePreviewFunctionalTestBase.php +++ b/tests/src/Functional/EntityReferencePreviewFunctionalTestBase.php @@ -73,7 +73,7 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { /** * {@inheritdoc} */ - public static $modules = [ + protected static $modules = [ 'content_moderation', 'block', 'node', @@ -86,7 +86,7 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { /** * {@inheritdoc} */ - protected function setUp($import_test_views = TRUE) { + protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void { parent::setUp($import_test_views); $this->workflow = $this->createEditorialWorkflow(); $this->adminUser = $this->drupalCreateUser($this->adminPermissions); @@ -119,7 +119,7 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { 'name' => $content_type_name, 'type' => $content_type_id, ]; - $this->drupalPostForm(NULL, $edit, t('Save content type')); + $this->submitForm($edit, t('Save content type')); // Check the content type has been set to create new revisions. $this->assertTrue(NodeType::load($content_type_id)->shouldCreateNewRevision()); @@ -139,9 +139,10 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { */ public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') { $this->drupalGet('/admin/config/workflow/workflows'); - $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id); + $this->assertSession()->linkByHrefExists('admin/config/workflow/workflows/manage/' . $workflow_id); $edit['bundles[' . $content_type_id . ']'] = TRUE; - $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save')); + $this->drupalGet('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node'); + $this->submitForm($edit, t('Save')); // Ensure the parent environment is up-to-date. // @see content_moderation_workflow_insert() \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); @@ -176,7 +177,8 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { protected function enableIndicatorThroughUi() { $edit = ['draft_indicator' => '1']; $url = Url::fromRoute('entity_reference_preview.settings'); - $this->drupalPostForm($url, $edit, t('Save configuration')); + $this->drupalGet($url); + $this->submitForm($edit, t('Save configuration')); } /** @@ -185,7 +187,8 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { protected function disableIndicatorThroughUi() { $edit = ['draft_indicator' => '0']; $url = Url::fromRoute('entity_reference_preview.settings'); - $this->drupalPostForm($url, $edit, t('Save configuration')); + $this->drupalGet($url); + $this->submitForm($edit, t('Save configuration')); } /** @@ -193,7 +196,8 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { */ protected function enablePreviewModeThroughUi() { $url = Url::fromRoute('entity_reference_preview.controls'); - $this->drupalPostForm($url, [], t('Start')); + $this->drupalGet($url); + $this->submitForm([], t('Start')); } /** @@ -201,7 +205,8 @@ abstract class EntityReferencePreviewFunctionalTestBase extends ViewTestBase { */ protected function disablePreviewModeThroughUi() { $url = Url::fromRoute('entity_reference_preview.controls'); - $this->drupalPostForm($url, [], t('Stop')); + $this->drupalGet($url); + $this->submitForm([], t('Stop')); } } diff --git a/tests/src/Functional/EntityReferencePreviewTest.php b/tests/src/Functional/EntityReferencePreviewTest.php index 7824d830a995724246c9fb948df97517bf424b29..1b5b625fc6deca7a9bb564e4db52fbb7931bc059 100644 --- a/tests/src/Functional/EntityReferencePreviewTest.php +++ b/tests/src/Functional/EntityReferencePreviewTest.php @@ -16,7 +16,7 @@ class EntityReferencePreviewTest extends EntityReferencePreviewFunctionalTestBas /** * {@inheritdoc} */ - protected function setUp($import_test_views = TRUE) { + protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void { parent::setUp($import_test_views); $this->drupalLogin($this->adminUser); diff --git a/tests/src/Functional/EntityReferencePreviewViewsTest.php b/tests/src/Functional/EntityReferencePreviewViewsTest.php index 88ea47d31faf8c1e8858b0e8c153eeef2a62b23e..4a871e55055b322d64a93a5763c230b283e1947d 100644 --- a/tests/src/Functional/EntityReferencePreviewViewsTest.php +++ b/tests/src/Functional/EntityReferencePreviewViewsTest.php @@ -22,7 +22,7 @@ class EntityReferencePreviewViewsTest extends EntityReferencePreviewFunctionalTe /** * {@inheritdoc} */ - protected function setUp($import_test_views = TRUE) { + protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void { parent::setUp($import_test_views); $this->drupalLogin($this->adminUser); diff --git a/tests/src/Kernel/Plugin/views/display_extender/EntityPreviewDisplayExtenderTest.php b/tests/src/Kernel/Plugin/views/display_extender/EntityPreviewDisplayExtenderTest.php index 88144549f9a1d44676626281db5a42766ffd0123..0d04863d3b62c81bf207cd3a8da74747d87e667d 100644 --- a/tests/src/Kernel/Plugin/views/display_extender/EntityPreviewDisplayExtenderTest.php +++ b/tests/src/Kernel/Plugin/views/display_extender/EntityPreviewDisplayExtenderTest.php @@ -15,7 +15,7 @@ class EntityPreviewDisplayExtenderTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['system', 'views']; + protected static $modules = ['system', 'views']; /** * Data provider for testOptionsSummary. @@ -113,7 +113,7 @@ class EntityPreviewDisplayExtenderTest extends KernelTestBase { $extender = new EntityPreviewDisplayExtender([], 'entity_preview', []); $sections = []; $extender->defaultableSections($sections); - $this->assertEqual([ + $this->assertEquals([ 'entity_reference_preview_enable' => ['entity_reference_preview_enable'], ], $sections); } diff --git a/tests/src/Unit/Events/PreviewNegotiationSubscriberTest.php b/tests/src/Unit/Events/PreviewNegotiationSubscriberTest.php index 851de79a97b6c1b0051d5b7355dd29cbe196a5ff..de55c01e9fafda5df4f92bfe12657cc85d9f9ccf 100644 --- a/tests/src/Unit/Events/PreviewNegotiationSubscriberTest.php +++ b/tests/src/Unit/Events/PreviewNegotiationSubscriberTest.php @@ -2,12 +2,13 @@ namespace Drupal\Tests\entity_reference_preview\Unit\Events; +use Prophecy\PhpUnit\ProphecyTrait; use Drupal\entity_reference_preview\Events\PreviewNegotiationSubscriber; use Drupal\entity_reference_preview\PreviewDetectorPluginManager; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\RequestEvent /** * @coversDefaultClass \Drupal\entity_reference_preview\Events\PreviewNegotiationSubscriber @@ -17,6 +18,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent; */ class PreviewNegotiationSubscriberTest extends UnitTestCase { + use ProphecyTrait; /** * The system under test. * @@ -44,7 +46,7 @@ class PreviewNegotiationSubscriberTest extends UnitTestCase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { $this->previewDetector = $this->prophesize(PreviewDetectorPluginManager::class); $this->eventSubscriber = new PreviewNegotiationSubscriber( $this->previewDetector->reveal() @@ -64,7 +66,7 @@ class PreviewNegotiationSubscriberTest extends UnitTestCase { ->willReturn($active_detector) ->shouldBecalled($this->once()); $request = new Request(); - $event = $this->prophesize(GetResponseEvent::class); + $event = $this->prophesize(RequestEvent::class); $event->getRequest()->willReturn($request)->shouldBeCalled($this->once()); $this->eventSubscriber->setPreview($event->reveal()); $flag = $request->attributes diff --git a/tests/src/Unit/Plugin/Block/PreviewDetectorBlockTest.php b/tests/src/Unit/Plugin/Block/PreviewDetectorBlockTest.php index 1a07dd91854ee2fd07add9ca7352a0dba4b97484..4d2e61e87f8cc2472b34da249b601c6c599516fa 100644 --- a/tests/src/Unit/Plugin/Block/PreviewDetectorBlockTest.php +++ b/tests/src/Unit/Plugin/Block/PreviewDetectorBlockTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\entity_reference_preview\Unit\Plugin\Block; +use Prophecy\PhpUnit\ProphecyTrait; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\entity_reference_preview\Form\PreviewActionsForm; @@ -17,6 +18,7 @@ use Prophecy\Argument; */ class PreviewDetectorBlockTest extends UnitTestCase { + use ProphecyTrait; /** * The system under test. * @@ -48,7 +50,7 @@ class PreviewDetectorBlockTest extends UnitTestCase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { $this->formBuilder = $this->prophesize(FormBuilderInterface::class); $this->isPreviewing = (bool) mt_rand(0, 1); $this->previewingViaCookie = (bool) mt_rand(0, 1); @@ -86,7 +88,7 @@ class PreviewDetectorBlockTest extends UnitTestCase { * @covers ::getCacheContexts */ public function testGetCacheContexts() { - $this->assertArrayEquals( + $this->assertEquals( ['entity_reference_preview'], $this->block->getCacheContexts() ); diff --git a/tests/src/Unit/Plugin/PreviewDetector/RenderedEntityPreviewDetectorTest.php b/tests/src/Unit/Plugin/PreviewDetector/RenderedEntityPreviewDetectorTest.php index 4d8b96f36f551f66035b3d660c52af96337d6faa..05cc1b56400bac3864d4010a9c937019d09801de 100644 --- a/tests/src/Unit/Plugin/PreviewDetector/RenderedEntityPreviewDetectorTest.php +++ b/tests/src/Unit/Plugin/PreviewDetector/RenderedEntityPreviewDetectorTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\entity_reference_preview\Unit\Plugin\PreviewDetector; +use Prophecy\PhpUnit\ProphecyTrait; use Drupal\entity_reference_preview\Plugin\PreviewDetector\RenderedEntityPreviewDetector; use Drupal\node\NodeInterface; use Drupal\Tests\UnitTestCase; @@ -13,6 +14,7 @@ use Symfony\Component\HttpFoundation\Request; */ class RenderedEntityPreviewDetectorTest extends UnitTestCase { + use ProphecyTrait; /** * Data provider for testIsPreviewing. *