Skip to content

Commit

Permalink
Updated tests and plugin annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 19, 2024
1 parent 5b3a421 commit 653efa0
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* Form controller for the press entity entity edit forms.
*
* @codeCoverageIgnore
*/
final class PressEntityForm extends ContentEntityForm {

Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Form handler for press entity type forms.
*
* @codeCoverageIgnore
*/
final class PressEntityTypeForm extends BundleEntityFormBase {

Expand Down Expand Up @@ -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),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,8 @@
/**
* Provides a sup_social_embedded_media plugin.
*
* @codeCoverageIgnore
*
* Usage:
*
* @code
Expand All @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

/**
* Defines the access control handler for the Press entity type.
*
* @codeCoverageIgnore
*/
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* Provides a list controller for the press entity entity type.
*
* @codeCoverageIgnore
*/
final class PressEntityListBuilder extends EntityListBuilder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\Tests\supress_helper\Kernel\Entity;

use Drupal\KernelTests\KernelTestBase;

/**
* @coversDefaultClass \Drupal\supress_helper\Entity\PressAward
*/
class PressEntityTest extends KernelTestBase {

protected static $modules = [
'supress_helper',
'system',
'field',
'user',
'migrate',
];

protected function setUp(): void {
parent::setUp();
$this->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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

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;
use Drupal\media\MediaInterface;
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;

/**
Expand All @@ -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));

Expand All @@ -61,4 +73,4 @@ public function testEntityInsertEvent() {
$this->assertNull($this->eventSubscriber->onEntityCreate($event));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(), [], '', []);
Expand Down

0 comments on commit 653efa0

Please sign in to comment.