Skip to content

Commit

Permalink
Merge pull request #916 from City-of-Helsinki/UHF-11069
Browse files Browse the repository at this point in the history
UHF-11069 Unit tests for blocks
  • Loading branch information
khalima authored Mar 7, 2025
2 parents 9a1995e + 64ec0bc commit ddc2ac4
Show file tree
Hide file tree
Showing 20 changed files with 1,634 additions and 55 deletions.
10 changes: 5 additions & 5 deletions src/Plugin/Block/ContentBlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function create(
array $configuration,
$plugin_id,
$plugin_definition,
) : static {
): static {
return new static(
$configuration,
$plugin_id,
Expand All @@ -77,7 +77,7 @@ public static function create(
/**
* {@inheritdoc}
*/
public function getCacheTags() : array {
public function getCacheTags(): array {
$matcher = $this->entityVersionMatcher->getType();

if (
Expand All @@ -95,21 +95,21 @@ public function getCacheTags() : array {
/**
* {@inheritDoc}
*/
public function getCacheContexts() : array {
public function getCacheContexts(): array {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}

/**
* {@inheritdoc}
*/
public function build() : array {
public function build(): array {
return [];
}

/**
* Return current entity version.
*/
protected function getCurrentEntityVersion() : array {
protected function getCurrentEntityVersion(): array {
return $this->entityVersionMatcher->getType();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Block/HeroBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HeroBlock extends ContentBlockBase {
/**
* {@inheritdoc}
*/
public function build() : array {
public function build(): array {
$build = [];

/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
Expand Down
25 changes: 13 additions & 12 deletions src/Plugin/Block/IbmChatApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Provides a Watson chatbot block.
*
* @Block(
* id = "ibm_chat_app",
* admin_label = @Translation("IBM Chat App"),
* )
*/
#[Block(
id: "ibm_chat_app",
admin_label: new TranslatableMarkup("IBM Chat App"),
)]
class IbmChatApp extends BlockBase {

/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
public function blockForm($form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();

Expand Down Expand Up @@ -58,17 +59,17 @@ public function blockForm($form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState) {
$this->configuration['hostname'] = $formState->getValue('hostname');
$this->configuration['engagementId'] = $formState->getValue('engagementId');
$this->configuration['tenantId'] = $formState->getValue('tenantId');
$this->configuration['assistantId'] = $formState->getValue('assistantId');
public function blockSubmit($form, FormStateInterface $form_state): void {
$this->configuration['hostname'] = $form_state->getValue('hostname');
$this->configuration['engagementId'] = $form_state->getValue('engagementId');
$this->configuration['tenantId'] = $form_state->getValue('tenantId');
$this->configuration['assistantId'] = $form_state->getValue('assistantId');
}

/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {

$library = ['helfi_platform_config/chat_enhancer'];

Expand Down
13 changes: 7 additions & 6 deletions src/Plugin/Block/LowerContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\helfi_tpr\Entity\Service;

/**
* Provides a 'LowerContentBlock' block.
*
* @Block(
* id = "lower_content_block",
* admin_label = @Translation("Lower content block"),
* )
*/
#[Block(
id: "lower_content_block",
admin_label: new TranslatableMarkup("Lower content block"),
)]
class LowerContentBlock extends ContentBlockBase {

/**
* {@inheritdoc}
*/
public function build() : array {
public function build(): array {
$build['lower_content'] = [
'#theme' => 'lower_content_block',
'#title' => $this->t('Lower content block'),
Expand Down
13 changes: 7 additions & 6 deletions src/Plugin/Block/ProfileBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a Profile Block.
*
* @Block(
* id = "profile_block",
* admin_label = @Translation("Profile block"),
* category = @Translation("Profile"),
* )
*/
#[Block(
id: "profile_block",
admin_label: new TranslatableMarkup("Profile block"),
category: new TranslatableMarkup("Profile"),
)]
final class ProfileBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Plugin/Block/ReactAndShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a 'ReactAndShare' block.
*
* @Block(
* id = "react_and_share",
* admin_label = @Translation("React and Share"),
* )
*/
#[Block(
id: "react_and_share",
admin_label: new TranslatableMarkup("React and Share"),
)]
final class ReactAndShare extends BlockBase implements ContainerFactoryPluginInterface {

/**
Expand All @@ -35,18 +36,17 @@ public static function create(
array $configuration,
$plugin_id,
$plugin_definition,
) : self {
): self {
$instance = new self($configuration, $plugin_id, $plugin_definition);
assert($container->get('language_manager') instanceof ConfigurableLanguageManagerInterface);

$instance->languageManager = $container->get('language_manager');
return $instance;
}

/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {
$langcode = $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
Expand Down
13 changes: 7 additions & 6 deletions src/Plugin/Block/SidebarContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\helfi_tpr\Entity\Service;

/**
* Provides a 'SidebarContentBlock' block.
*
* @Block(
* id = "sidebar_content_block",
* admin_label = @Translation("Sidebar content block"),
* )
*/
#[Block(
id: "sidebar_content_block",
admin_label: new TranslatableMarkup("Sidebar content block"),
)]
class SidebarContentBlock extends ContentBlockBase {

/**
* {@inheritdoc}
*/
public function build() : array {
public function build(): array {
$build['sidebar_content'] = [
'#theme' => 'sidebar_content_block',
'#title' => $this->t('Sidebar content block'),
Expand Down
13 changes: 7 additions & 6 deletions src/Plugin/Block/SocialMediaSharingBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Drupal\helfi_platform_config\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\social_media\Event\SocialMediaEvent;
use Drupal\social_media\Plugin\Block\SocialSharingBlock;
Expand All @@ -15,12 +17,11 @@
* Provides a 'SocialMediaSharingBlock' block.
*
* @todo Rewrite the block. Get rid of social_media module.
*
* @Block(
* id = "helfi_platform_config_social_sharing_block",
* admin_label = @Translation("Social Media Sharing block"),
* )
*/
#[Block(
id: "helfi_platform_config_social_sharing_block",
admin_label: new TranslatableMarkup("Social Media Sharing block"),
)]
class SocialMediaSharingBlock extends SocialSharingBlock {

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public static function create(
/**
* {@inheritdoc}
*/
public function build() : array {
public function build(): array {

$build = [];

Expand Down
10 changes: 5 additions & 5 deletions src/Plugin/Block/TeliaAceWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TeliaAceWidget extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
public function blockForm($form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();

Expand All @@ -53,15 +53,15 @@ public function blockForm($form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState) {
$this->configuration['chat_id'] = $formState->getValue('chat_id');
$this->configuration['chat_title'] = $formState->getValue('chat_title');
public function blockSubmit($form, FormStateInterface $form_state): void {
$this->configuration['chat_id'] = $form_state->getValue('chat_id');
$this->configuration['chat_title'] = $form_state->getValue('chat_title');
}

/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {

$build = [];

Expand Down
79 changes: 79 additions & 0 deletions tests/src/Unit/Block/BlockUnitTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_platform_config\Unit\Block;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Unit test base for blocks.
*
* @group helfi_platform_config
*/
class BlockUnitTestBase extends UnitTestCase {

use StringTranslationTrait;

/**
* The entity type manager mock.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected EntityTypeManagerInterface|MockObject $entityTypeManager;

/**
* The entity version matcher mock.
*
* @var \Drupal\helfi_platform_config\EntityVersionMatcher|\PHPUnit\Framework\MockObject\MockObject
*/
protected EntityVersionMatcher|MockObject $entityVersionMatcher;

/**
* The module handler mock.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected ModuleHandlerInterface|MockObject $moduleHandler;

/**
* The mocked translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $stringTranslation;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$this->entityVersionMatcher = $this->createMock(EntityVersionMatcher::class);
$this->moduleHandler = $this->createMock(ModuleHandlerInterface::class);

$this->stringTranslation = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface');
}

/**
* Helper method to translate strings for tests.
*
* @param string $string
* The string to translate.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* Returns translatable markup.
*/
protected function translate(string $string): TranslatableMarkup {
// phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
return new TranslatableMarkup($string, string_translation: $this->stringTranslation);
}

}
Loading

0 comments on commit ddc2ac4

Please sign in to comment.