Skip to content

Commit

Permalink
Merge pull request #319 from localgovdrupal/fix/1.x-318/translation-s…
Browse files Browse the repository at this point in the history
…upport

Fix Translation for alert banner not displaying
  • Loading branch information
andybroomfield committed May 21, 2024
2 parents dec5e46 + d2df424 commit e4aff62
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Entity/AlertBannerEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Title'))
->setDescription(t('The title of the Alert banner.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
Expand Down
18 changes: 16 additions & 2 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Expand Down Expand Up @@ -36,6 +37,13 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf
*/
protected $currentUser;

/**
* The entity repository services.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* Constructs a new AlertBannerBlock.
*
Expand All @@ -49,11 +57,14 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf
* The entity type manager service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* Current user service.
* @param Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->entityRepository = $entity_repository;
}

/**
Expand All @@ -76,7 +87,8 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('current_user')
$container->get('current_user'),
$container->get('entity.repository'),
);
}

Expand Down Expand Up @@ -191,6 +203,8 @@ protected function getCurrentAlertBanners() {
// Visibility check happens in build, so we get cache contexts on all.
foreach ($published_alert_banners as $alert_banner_id) {
$alert_banner = $this->entityTypeManager->getStorage('localgov_alert_banner')->load($alert_banner_id);
$alert_banner = $this->entityRepository->getTranslationFromContext($alert_banner);

$is_accessible = $alert_banner->access('view', $this->currentUser);
if ($is_accessible) {
$current_alert_banners[] = $alert_banner;
Expand Down
240 changes: 240 additions & 0 deletions tests/src/Functional/TranslationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php

namespace Drupal\Tests\localgov_alert_banner\Functional;

use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;

/**
* Functional tests for LocalGovDrupal Alert banner block.
*/
class TranslationsTest extends BrowserTestBase {

use NodeCreationTrait;

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'content_translation',
'path',
'path_alias',
'options',
'node',
'language',
'locale',
'localgov_alert_banner',
];

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

$this->drupalPlaceBlock('localgov_alert_banner_block');
ConfigurableLanguage::createFromLangcode('zz')->save();
$this->container->get('content_translation.manager')->setEnabled('localgov_alert_banner', 'localgov_alert_banner', TRUE);
FieldConfig::loadByName('localgov_alert_banner', 'localgov_alert_banner', 'short_description')->setTranslatable(TRUE)->save();
}

/**
* Test that valid translation is brought back based on current language.
*/
public function testAlertBannerTranslation(): void {

$default_langcode = $this->container->get('language.default')->get()->getId();

// Create alert banner.
$alert_title = 'home page alert title - ' . $this->randomMachineName(8);
$alert_body = 'home page alert body - ' . $this->randomMachineName(32);
$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $alert_title,
'short_description' => $alert_body,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
'langcode' => $default_langcode,
]);
$alert->save();

// Create translation.
$translated_alert_title = 'translated home page alert title - ' . $this->randomMachineName(8);
$translated_alert_body = 'translated home page alert body - ' . $this->randomMachineName(32);
$alert->addTranslation('zz', [
'title' => $translated_alert_title,
'short_description' => $translated_alert_body,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
])->save();

// Test on home page.
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains($alert_title);
$this->assertSession()->pageTextContains($alert_body);
$this->assertSession()->pageTextNotContains($translated_alert_title);
$this->assertSession()->pageTextNotContains($translated_alert_body);

// Switch language.
$this->drupalGet('/zz');

// Test correct translation on home page.
$this->assertSession()->pageTextNotContains($alert_title);
$this->assertSession()->pageTextNotContains($alert_body);
$this->assertSession()->pageTextContains($translated_alert_title);
$this->assertSession()->pageTextContains($translated_alert_body);

// Create node.
$this->drupalCreateContentType(['type' => 'page']);
$page = $this->createNode([
'type' => 'page',
'title' => $this->randomMachineName(8),
'status' => NodeInterface::PUBLISHED,
'langcode' => $default_langcode,
]);

// Add node translation.
$page->addTranslation('zz', [
'title' => $this->randomMachineName(8),
'status' => NodeInterface::PUBLISHED,
]);

// Create banner with page restriction.
$alert_title_node = 'node 1 alert title - ' . $this->randomMachineName(8);
$alert_body_node = 'node 1 alert body - ' . $this->randomMachineName(32);
$node_alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $alert_title_node,
'short_description' => $alert_body_node,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
'langcode' => $default_langcode,
'visibility' => [
'conditions' => [
'request_path' => [
'pages' => '/node/1',
'negate' => 0,
],
],
],
]);
$node_alert->save();

// Translate banner with page restriction.
$translated_alert_title_node = 'translated node 1 alert title - ' . $this->randomMachineName(8);
$translated_alert_body_node = 'translated node 1 alert body - ' . $this->randomMachineName(32);
$node_alert->addTranslation('zz', [
'title' => $translated_alert_title_node,
'short_description' => $translated_alert_body_node,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
])->save();

// Go to node in default language.
$this->drupalGet('/node/1');

// Test correct translation appears.
$this->assertSession()->pageTextContains($alert_title_node);
$this->assertSession()->pageTextContains($alert_body_node);
$this->assertSession()->pageTextNotContains($translated_alert_title_node);
$this->assertSession()->pageTextNotContains($translated_alert_body_node);

// Change language.
$this->drupalGet('/zz/node/1');

// Test correct translation appears.
$this->assertSession()->pageTextNotContains($alert_title_node);
$this->assertSession()->pageTextNotContains($alert_body_node);
$this->assertSession()->pageTextContains($translated_alert_title_node);
$this->assertSession()->pageTextContains($translated_alert_body_node);

// Create node with path.
$page = $this->createNode([
'type' => 'page',
'title' => $this->randomMachineName(8),
'status' => NodeInterface::PUBLISHED,
'langcode' => $default_langcode,
]);
$this->container->get('entity_type.manager')->getStorage('path_alias')->create([
'path' => '/node/' . $page->id(),
'alias' => '/untranslated-path',
'langcode' => $default_langcode,
])->save();

// Add node translation.
$page->addTranslation('zz', [
'title' => $this->randomMachineName(8),
'status' => NodeInterface::PUBLISHED,
]);
$this->container->get('entity_type.manager')->getStorage('path_alias')->create([
'path' => '/node/' . $page->id(),
'alias' => '/translated-path',
'langcode' => 'zz',
])->save();

// Enable translation of the visibility field.
FieldConfig::loadByName('localgov_alert_banner', 'localgov_alert_banner', 'visibility')->setTranslatable(TRUE)->save();

// Create banner with page restriction.
$alert_title_pa_node = 'path alias page alert title - ' . $this->randomMachineName(8);
$alert_body_pa_node = 'path alias page alert body - ' . $this->randomMachineName(32);
$pa_node_alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $alert_title_pa_node,
'short_description' => $alert_body_pa_node,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
'langcode' => $default_langcode,
'visibility' => [
'conditions' => [
'request_path' => [
'pages' => '/untranslated-path',
'negate' => 0,
],
],
],
]);
$node_alert->save();

// Translate banner with page restriction.
$translated_alert_title_pa_node = 'translated path alias page alert title - ' . $this->randomMachineName(8);
$translated_alert_body_pa_node = 'translated path alias page alert body - ' . $this->randomMachineName(32);
$pa_node_alert->addTranslation('zz', [
'title' => $translated_alert_title_pa_node,
'short_description' => $translated_alert_body_pa_node,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
])->save();

// Go to node in default language.
$this->drupalGet('/untranslated-path');

// Test correct translation appears.
$this->assertSession()->pageTextContains($alert_title_pa_node);
$this->assertSession()->pageTextContains($alert_body_pa_node);
$this->assertSession()->pageTextNotContains($translated_alert_title_pa_node);
$this->assertSession()->pageTextNotContains($translated_alert_body_pa_node);

// Change language.
$this->drupalGet('/zz/translated-path');

// Test correct translation appears.
$this->assertSession()->pageTextNotContains($alert_title_pa_node);
$this->assertSession()->pageTextNotContains($alert_body_pa_node);
$this->assertSession()->pageTextContains($translated_alert_title_pa_node);
$this->assertSession()->pageTextContains($translated_alert_body_pa_node);
}

}

0 comments on commit e4aff62

Please sign in to comment.