Skip to content

Commit

Permalink
Merge pull request #122 from City-of-Helsinki/UHF-5512-tpr-unit-highl…
Browse files Browse the repository at this point in the history
…ights

UHF-5512: Add highlight connection support for TPR Units
  • Loading branch information
tapsal authored May 30, 2022
2 parents fd68274 + 10194ee commit d9d4eb8
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 17 deletions.
1 change: 1 addition & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sonar.exclusions=tests/**
sonar.cpd.exclusions=src/Fixture/*
16 changes: 16 additions & 0 deletions helfi_tpr.install
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,19 @@ function helfi_tpr_update_8038() : void {
}
}
}

/**
* Add 'highlights' field to the TPR Unit entity.
*/
function helfi_tpr_update_8039() : void {
$fields['highlights'] = BaseFieldDefinition::create('tpr_connection')
->setLabel(new TranslatableMarkup('Highlights'))
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE);

foreach ($fields as $name => $field) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field);
}
}
24 changes: 17 additions & 7 deletions migrations/tpr_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,27 @@ process:
id: id
name: name
services: service_descriptions
_opening_hours_connections:
plugin: array_element_equals
source: connections
value: OPENING_HOURS
key: type
opening_hours:
plugin: sub_process
source: '@_opening_hours_connections'
process:
value: value
data: data
type: type
_highlight_connections:
plugin: array_element_equals
source: connections
value: HIGHLIGHT
key: type
highlights:
plugin: sub_process
source: '@_highlight_connections'
process:
# Only import opening hours
skip:
plugin: skip_on_value
not_equals: true
source: type
value: OPENING_HOURS
method: row
value: value
data: data
type: type
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(new TranslatableMarkup('Show website link'))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['highlights'] = BaseFieldDefinition::create('tpr_connection')
->setLabel(new TranslatableMarkup('Highlights'))
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('view', TRUE);

return $fields;
}
Expand Down
41 changes: 41 additions & 0 deletions src/Field/Connection/Highlight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_tpr\Field\Connection;

use Drupal\Component\Utility\Html;

/**
* Provides a domain object for TPR connection type of HIGHLIGHT.
*/
final class Highlight extends Connection {

/**
* The type name.
*
* @var string
*/
public const TYPE_NAME = 'HIGHLIGHT';

/**
* {@inheritdoc}
*/
public function getFields(): array {
return [
'name',
];
}

/**
* {@inheritdoc}
*/
public function build(): array {
return [
'name' => [
'#markup' => Html::escape($this->get('name')),
],
];
}

}
1 change: 1 addition & 0 deletions src/Field/Connection/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class Repository {
*/
protected array $map = [
OpeningHour::TYPE_NAME => OpeningHour::class,
Highlight::TYPE_NAME => Highlight::class,
];

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Fixture/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,16 @@ public function getMockData() : array {
[
'unit_id' => 1,
'section_type' => 'HIGHLIGHT',
'name_fi' => 'hilight fi 1',
'name_en' => 'hilight en 1',
'name_sv' => 'hilight sv 1',
'name_fi' => 'highlight fi 1',
'name_en' => 'highlight en 1',
'name_sv' => 'highlight sv 1',
],
[
'unit_id' => 1,
'section_type' => 'HIGHLIGHT',
'name_fi' => 'hilight fi 2',
'name_en' => 'hilight en 2',
'name_sv' => 'hilight sv 2',
'name_fi' => 'highlight fi 2',
'name_en' => 'highlight en 2',
'name_sv' => 'highlight sv 2',
],
[
'unit_id' => 1,
Expand Down
59 changes: 59 additions & 0 deletions src/Plugin/migrate/process/ArrayElementEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_tpr\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
* Only include array when element (with a given key) matches a given value.
*
* @MigrateProcessPlugin(
* id = "array_element_equals"
* )
*
* @code
* opening_hours_connections:
* plugin: array_element_equals
* source: connections
* value: OPENING_HOURS
* key: type
* @endcode
*/
class ArrayElementEquals extends ProcessPluginBase {

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (empty($configuration['value']) && !array_key_exists('value', $configuration)) {
throw new \InvalidArgumentException('ArrayElementEquals plugin is missing value configuration.');
}
if (empty($configuration['key']) && !array_key_exists('key', $configuration)) {
throw new \InvalidArgumentException('ArrayElementEquals plugin is missing key configuration.');
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) : array {
$matchValue = $this->configuration['value'];
$key = $this->configuration['key'];

if (empty($value) || !is_array($value)) {
return [];
}

if (!empty($value[$key]) && $value[$key] === $matchValue) {
return $value;
}

return [];
}

}
19 changes: 16 additions & 3 deletions tests/src/Functional/ConnectionFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\Core\Url;
use Drupal\helfi_tpr\Entity\TprEntityBase;
use Drupal\helfi_tpr\Field\Connection\Highlight;
use Drupal\helfi_tpr\Field\Connection\OpeningHour;

/**
Expand All @@ -25,14 +26,21 @@ protected function getEntity(): TprEntityBase {
$openingHour = new OpeningHour();
$openingHour->set('name', "Open $language 1");

$field = $entity->getTranslation($language)
$openingHoursField = $entity->getTranslation($language)
->get('opening_hours');
$field->appendItem($openingHour);
$openingHoursField->appendItem($openingHour);

$openingHour = new OpeningHour();
$openingHour->set('www', "https://localhost/$language")
->set('name', "Open $language 2");
$field->appendItem($openingHour);
$openingHoursField->appendItem($openingHour);

$highlight = new Highlight();
$highlight->set('name', "Highlight $language");
$highlightsField = $entity->getTranslation($language)
->get('highlights');
$highlightsField->appendItem($highlight);

$entity->save();
}

Expand All @@ -55,6 +63,7 @@ private function assertFieldDisplay(bool $expect_visible) : void {
'Opening hours',
"Open $language 1",
"Open $language 2",
"Highlight $language",
];

foreach ($strings as $string) {
Expand Down Expand Up @@ -83,6 +92,10 @@ public function testFormatter() : void {
'type' => 'tpr_connection',
'label' => 'above',
])
->setComponent('highlights', [
'type' => 'tpr_connection',
'label' => 'above',
])
->save();

// Make sure we can see connections field for all languages once
Expand Down
1 change: 0 additions & 1 deletion tests/src/Functional/CustomFieldFormatterTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function setUp() : void {

$account = $this->createUser($this->getUserPermissions());
$this->drupalLogin($account);

}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/src/Kernel/UnitMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\Tests\helfi_tpr\Kernel;

use Drupal\helfi_tpr\Entity\Unit;
use Drupal\helfi_tpr\Field\Connection\Highlight;
use Drupal\helfi_tpr\Field\Connection\OpeningHour;

/**
Expand Down Expand Up @@ -64,6 +65,9 @@ public function testUnitMigration() : void {
$opening_hour = $translation->get('opening_hours')->get(1)->data;
$this->assertInstanceOf(OpeningHour::class, $opening_hour);
$this->assertEquals("https://localhost/$langcode", $opening_hour->get('www'));
$highlight = $translation->get('highlights')->get(0)->data;
$this->assertInstanceOf(Highlight::class, $highlight);
$this->assertEquals("highlight $langcode 1", $highlight->get('name'));
}

// Re-run migrate and make sure migrate map hash doesn't change.
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Unit/ConnectionRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\Tests\helfi_tpr\Unit;

use Drupal\helfi_tpr\Field\Connection\Connection;
use Drupal\helfi_tpr\Field\Connection\Highlight;
use Drupal\helfi_tpr\Field\Connection\OpeningHour;
use Drupal\helfi_tpr\Field\Connection\Repository;
use Drupal\Tests\UnitTestCase;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function testGet(string $type) : void {
public function getTestData() : array {
return [
[OpeningHour::TYPE_NAME],
[Highlight::TYPE_NAME],
];
}

Expand Down
24 changes: 24 additions & 0 deletions tests/src/Unit/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\helfi_tpr\Unit;

use Drupal\helfi_tpr\Field\Connection\Highlight;
use Drupal\helfi_tpr\Field\Connection\OpeningHour;
use Drupal\Tests\UnitTestCase;

Expand Down Expand Up @@ -42,6 +43,29 @@ public function testOpeningHours() : void {
$this->assertNotEmpty($object->build());
}

/**
* Tests highlights.
*
* @covers \Drupal\helfi_tpr\Field\Connection\Highlight::build
* @covers \Drupal\helfi_tpr\Field\Connection\Highlight::getFields
* @covers ::set
* @covers ::get
* @covers ::isValidField
*/
public function testHighlights() : void {
$object = new Highlight();
$this->assertEquals('HIGHLIGHT', $object::TYPE_NAME);
$object->set('name', 'Some information.');
$this->assertNotEmpty($object->build());

// Make sure we can override data.
$object->set('name', 'override');
$this->assertNotEmpty($object->build());
$this->assertEquals('override', $object->get('name'));

$this->assertEquals(['name'], $object->getFields());
}

/**
* Tests invalid field name.
*
Expand Down

0 comments on commit d9d4eb8

Please sign in to comment.