From 653efa03dce4397b12a4149042dec9b18e652729 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Tue, 19 Nov 2024 08:04:17 -0800 Subject: [PATCH] Updated tests and plugin annotations --- .../SuPressEventSubscriber.php | 2 +- .../src/Form/PressEntityForm.php | 14 +++++-- .../src/Form/PressEntityTypeForm.php | 4 +- .../QueueWorker/BookAncillaryMigrator.php | 13 +++--- .../QueueWorker/BookCoverDownloader.php | 26 ++++-------- .../src/Plugin/QueueWorker/ImageResize.php | 13 +++--- .../process/SupSocialEmbeddedMedia.php | 8 +++- .../src/PressAccessControlHandler.php | 3 +- .../src/PressEntityListBuilder.php | 2 + .../src/PressEntityTypeListBuilder.php | 2 + .../src/Kernel/Entity/PressEntityTest.php | 40 +++++++++++++++++++ .../SuPressEventSubscriberTest.php | 18 +++++++-- .../QueueWorker/BookCoverDownloaderTest.php | 6 +++ 13 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Kernel/Entity/PressEntityTest.php diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/EventSubscriber/SuPressEventSubscriber.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/EventSubscriber/SuPressEventSubscriber.php index a2c826429..d148520ee 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/EventSubscriber/SuPressEventSubscriber.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/EventSubscriber/SuPressEventSubscriber.php @@ -41,7 +41,7 @@ public static function getSubscribedEvents(): array { */ public function onNextEntityAction(EntityActionEvent $event) { $entity = $event->getEntity(); - if ($entity->getEntityTypeId() == 'presss') { + if ($entity->getEntityTypeId() == 'press') { $bundle = $entity->bundle(); $uuid = $entity->uuid(); $event->setEntityUrl("/tags/$bundle:$uuid"); diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityForm.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityForm.php index d5183d61a..da255af4a 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityForm.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityForm.php @@ -9,6 +9,8 @@ /** * Form controller for the press entity entity edit forms. + * + * @codeCoverageIgnore */ final class PressEntityForm extends ContentEntityForm { @@ -26,13 +28,17 @@ public function save(array $form, FormStateInterface $form_state): int { switch ($result) { case SAVED_NEW: - $this->messenger()->addStatus($this->t('New press entity %label has been created.', $message_args)); - $this->logger('supress_helper')->notice('New press entity %label has been created.', $logger_args); + $this->messenger() + ->addStatus($this->t('New press entity %label has been created.', $message_args)); + $this->logger('supress_helper') + ->notice('New press entity %label has been created.', $logger_args); break; case SAVED_UPDATED: - $this->messenger()->addStatus($this->t('The press entity %label has been updated.', $message_args)); - $this->logger('supress_helper')->notice('The press entity %label has been updated.', $logger_args); + $this->messenger() + ->addStatus($this->t('The press entity %label has been updated.', $message_args)); + $this->logger('supress_helper') + ->notice('The press entity %label has been updated.', $logger_args); break; default: diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityTypeForm.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityTypeForm.php index c6f54d0df..607adf6e4 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityTypeForm.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Form/PressEntityTypeForm.php @@ -11,6 +11,8 @@ /** * Form handler for press entity type forms. + * + * @codeCoverageIgnore */ final class PressEntityTypeForm extends BundleEntityFormBase { @@ -64,7 +66,7 @@ public function save(array $form, FormStateInterface $form_state): int { $message_args = ['%label' => $this->entity->label()]; $this->messenger()->addStatus( - match($result) { + match ($result) { SAVED_NEW => $this->t('The press entity type %label has been added.', $message_args), SAVED_UPDATED => $this->t('The press entity type %label has been updated.', $message_args), } diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookAncillaryMigrator.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookAncillaryMigrator.php index 8313cd33d..108d9e7c3 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookAncillaryMigrator.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookAncillaryMigrator.php @@ -6,7 +6,9 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Queue\Attribute\QueueWorker; use Drupal\Core\Queue\QueueWorkerBase; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\file\FileInterface; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -15,13 +17,12 @@ * Cron queue worker for migrating book ancillary contents. * * @codeCoverageIgnore One time use for migration. - * - * @QueueWorker( - * id = "press_book_ancillary_migrator", - * title = @Translation("Book cover downloader"), - * cron = {"time" = 60} - * ) */ +#[QueueWorker( + id: "press_book_ancillary_migrator", + title: new TranslatableMarkup("Book Ancillary Migrator"), + cron: ['time' => 100] +)] class BookAncillaryMigrator extends QueueWorkerBase implements ContainerFactoryPluginInterface { /** diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookCoverDownloader.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookCoverDownloader.php index 37f344622..04d8ab432 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookCoverDownloader.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/BookCoverDownloader.php @@ -8,7 +8,9 @@ use Drupal\Core\File\FileExists; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Queue\Attribute\QueueWorker; use Drupal\Core\Queue\QueueWorkerBase; +use Drupal\Core\StringTranslation\TranslatableMarkup; use GuzzleHttp\ClientInterface; use GuzzleHttp\Cookie\CookieJar; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -22,6 +24,11 @@ * cron = {"time" = 60} * ) */ +#[QueueWorker( + id: "press_cover_downloader", + title: new TranslatableMarkup("Book cover downloader"), + cron: ['time' => 100] +)] class BookCoverDownloader extends QueueWorkerBase implements ContainerFactoryPluginInterface { const FILE_DIRECTORY = 'public://media/covers'; @@ -211,23 +218,4 @@ protected function downloadImage(string $image_url, string $destination, int $wo return $media->id(); } - /** - * Resize the newly downloaded image, so it's not giant. - * - * @param $fid - * File entity id. - */ - protected function adjustCoverImageSize(int $fid) { - $image_style = $this->entityTypeManager->getStorage('image_style')::load("breakpoint_2xl_1x"); - $file = $this->entityTypeManager->getStorage("file")->load($fid); - $temp = "temporary://" . $file->label(); - $success = $image_style->createDerivative($file->getFileUri(), $temp); - if (!$success) { - $this->logger->get('supress')->error('Unable to generate image derivative for file @uri', ['@uri' => $file->getFileUri()]); - return; - } - $this->fileSystem->move($temp, $file->getFileUri(), FileExists::Replace); - $file->save(); - } - } diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/ImageResize.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/ImageResize.php index 1888f7031..3a7ffbb98 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/ImageResize.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/QueueWorker/ImageResize.php @@ -7,20 +7,21 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Queue\Attribute\QueueWorker; use Drupal\Core\Queue\QueueWorkerBase; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Cron queue worker for Resizing images * * @codeCoverageIgnore - * - * @QueueWorker( - * id = "press_image_resize", - * title = @Translation("Image Resize"), - * cron = {"time" = 60} - * ) */ +#[QueueWorker( + id: "press_image_resize", + title: new TranslatableMarkup("Image Resize"), + cron: ['time' => 100] +)] class ImageResize extends QueueWorkerBase implements ContainerFactoryPluginInterface { /** diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/migrate/process/SupSocialEmbeddedMedia.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/migrate/process/SupSocialEmbeddedMedia.php index 887636240..b8cc8c9dd 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/migrate/process/SupSocialEmbeddedMedia.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/Plugin/migrate/process/SupSocialEmbeddedMedia.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\migrate\Attribute\MigrateProcess; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; @@ -14,6 +15,8 @@ /** * Provides a sup_social_embedded_media plugin. * + * @codeCoverageIgnore + * * Usage: * * @code @@ -22,9 +25,10 @@ * plugin: sup_social_embedded_media * source: foo * @endcode - * - * @MigrateProcessPlugin(id = "sup_social_embedded_media") */ +#[MigrateProcess( + id: "sup_social_embedded_media" +)] final class SupSocialEmbeddedMedia extends ProcessPluginBase implements ContainerFactoryPluginInterface { /** diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressAccessControlHandler.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressAccessControlHandler.php index eaa7b3e64..847a9b06f 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressAccessControlHandler.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressAccessControlHandler.php @@ -9,6 +9,8 @@ /** * Defines the access control handler for the Press entity type. + * + * @codeCoverageIgnore */ class PressAccessControlHandler extends EntityAccessControlHandler { @@ -16,7 +18,6 @@ class PressAccessControlHandler extends EntityAccessControlHandler { * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { - \Drupal::logger('foo')->info($operation . ':' . $entity->uuid()); if ($account->hasPermission('administer press types')) { return AccessResult::allowed()->cachePerPermissions(); } diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityListBuilder.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityListBuilder.php index a6d4bb740..ff9449fd4 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityListBuilder.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityListBuilder.php @@ -9,6 +9,8 @@ /** * Provides a list controller for the press entity entity type. + * + * @codeCoverageIgnore */ final class PressEntityListBuilder extends EntityListBuilder { diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityTypeListBuilder.php b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityTypeListBuilder.php index b12ebc9a1..1bf30dcf2 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityTypeListBuilder.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/src/PressEntityTypeListBuilder.php @@ -12,6 +12,8 @@ * Defines a class to build a listing of press entity type entities. * * @see \Drupal\supress_helper\Entity\PressEntityType + * + * @codeCoverageIgnore */ final class PressEntityTypeListBuilder extends ConfigEntityListBuilder { diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Kernel/Entity/PressEntityTest.php b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Kernel/Entity/PressEntityTest.php new file mode 100644 index 000000000..91a377a4b --- /dev/null +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Kernel/Entity/PressEntityTest.php @@ -0,0 +1,40 @@ +installEntitySchema('user'); + $this->installEntitySchema('press_type'); + $this->installEntitySchema('press'); + + \Drupal::entityTypeManager() + ->getStorage('press_type') + ->create(['id' => 'price', 'label' => 'price']) + ->save(); + } + + public function testPressAwardCreation() { + $entity = \Drupal::entityTypeManager() + ->getStorage('press') + ->create(['title' => 'this is an award', 'bundle' => 'price']); + $entity->save(); + $this->assertGreaterThan(0, $entity->id()); + } + +} diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/EventSubscriber/SuPressEventSubscriberTest.php b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/EventSubscriber/SuPressEventSubscriberTest.php index 042ef93fc..c0dbca1be 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/EventSubscriber/SuPressEventSubscriberTest.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/EventSubscriber/SuPressEventSubscriberTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\supress_helper\Unit\EventSubscriber; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\core_event_dispatcher\Event\Entity\EntityCreateEvent; @@ -11,8 +12,8 @@ use Drupal\migrate\Plugin\MigrateIdMapInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Plugin\MigrationPluginManagerInterface; +use Drupal\next\Event\EntityActionEvent; use Drupal\supress_helper\EventSubscriber\SuPressEventSubscriber; -use Drupal\supress_helper\PressAwardInterface; use Drupal\Tests\UnitTestCase; /** @@ -37,13 +38,24 @@ protected function setUp(): void { $this->eventSubscriber = new SuPressEventSubscriber($entity_type_manager, $migration_manager); } + public function testNextEntityAction() { + $entity = $this->createMock(ContentEntityInterface::class); + $entity->method('getEntityTypeId')->willReturn('press'); + $entity->method('uuid')->willReturn('foobarbaz'); + $entity->method('bundle')->willReturn('foo'); + $event = new EntityActionEvent($entity, 'action', [], ''); + $this->eventSubscriber->onNextEntityAction($event); + $this->assertEquals('/tags/foo:foobarbaz', $event->getEntityUrl()); + } + public function testEntityInsertEvent() { $fieldItem = $this->createMock(FieldItemListInterface::class); $fieldItem->method('count')->willReturn(1); $fieldItem->method('getString')->willReturn('321'); - $entity = $this->createMock(PressAwardInterface::class); + $entity = $this->createMock(ContentEntityInterface::class); $entity->method('get')->willReturn($fieldItem); + $entity->method('getEntityTypeId')->willReturn('press'); $event = new EntityCreateEvent($entity); $this->assertNull($this->eventSubscriber->onEntityCreate($event)); @@ -61,4 +73,4 @@ public function testEntityInsertEvent() { $this->assertNull($this->eventSubscriber->onEntityCreate($event)); } -} \ No newline at end of file +} diff --git a/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/Plugin/QueueWorker/BookCoverDownloaderTest.php b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/Plugin/QueueWorker/BookCoverDownloaderTest.php index baed75d13..0a24a74e7 100644 --- a/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/Plugin/QueueWorker/BookCoverDownloaderTest.php +++ b/docroot/profiles/lagunita/supress/modules/supress_helper/tests/src/Unit/Plugin/QueueWorker/BookCoverDownloaderTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\supress_helper\Unit\Plugin\QueueWorker; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\image\ImageStyleInterface; use Drupal\supress_helper\Plugin\QueueWorker\BookCoverDownloader; use Drupal\Tests\UnitTestCase; use GuzzleHttp\Psr7\Stream; @@ -44,9 +45,14 @@ public function testBookCoverDownloader() { $entity = $this->createMock('\Drupal\file\FileInterface'); $entity->method('id')->willReturn(543); + $image_style = $this->createMock(ImageStyleInterface::class); + $image_style->method('createDerivative')->willReturn(TRUE); + $entity_storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface'); $entity_storage->method('create')->willReturn($entity); $entity_storage->method('getQuery')->willReturn($entity_query); + $entity_storage->method('load')->willReturn($image_style); + $this->entityTypeManager->method('getStorage')->wilLReturn($entity_storage); $worker = BookCoverDownloader::create($this->getDrupalContainer(), [], '', []);