diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 187aceb1..3a27a93f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,8 +65,11 @@ jobs: - name: Run PHPCS working-directory: ${{ env.DRUPAL_ROOT }} - run: | - vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal --extensions=php,module,inc,install,test,info + run: vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,info + + - name: Run phpstan + working-directory: ${{ env.DRUPAL_ROOT }} + run: vendor/bin/phpstan analyze -c $MODULE_FOLDER/phpstan.neon $MODULE_FOLDER - name: Start services working-directory: ${{ env.DRUPAL_ROOT }} diff --git a/helfi_tpr.module b/helfi_tpr.module index 761faba5..b1327646 100644 --- a/helfi_tpr.module +++ b/helfi_tpr.module @@ -10,6 +10,7 @@ declare(strict_types = 1); use Drupal\Core\Access\AccessResult; use Drupal\Core\Database\Query\AlterableInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; @@ -226,6 +227,7 @@ function template_preprocess_tpr_ontology_word_details(array &$variables) : void * Implements hook_ENTITY_TYPE_access(). */ function helfi_tpr_tpr_unit_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResult { + assert($entity instanceof EntityPublishedInterface); // Allow user to view unpublished Units based on permission. if ($operation === 'view' && !$entity->isPublished()) { return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_unit'); @@ -238,6 +240,7 @@ function helfi_tpr_tpr_unit_access(EntityInterface $entity, $operation, AccountI * Implements hook_ENTITY_TYPE_access(). */ function helfi_tpr_tpr_service_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResult { + assert($entity instanceof EntityPublishedInterface); // Allow user to view unpublished Services based on permission. if ($operation === 'view' && !$entity->isPublished()) { return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_service'); @@ -250,6 +253,7 @@ function helfi_tpr_tpr_service_access(EntityInterface $entity, $operation, Accou * Implements hook_ENTITY_TYPE_access(). */ function helfi_tpr_tpr_service_channel_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResult { + assert($entity instanceof EntityPublishedInterface); // Allow user to view unpublished Service channels based on permission. if ($operation === 'view' && !$entity->isPublished()) { return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_service_channel'); @@ -262,6 +266,7 @@ function helfi_tpr_tpr_service_channel_access(EntityInterface $entity, $operatio * Implements hook_ENTITY_TYPE_access(). */ function helfi_tpr_tpr_errand_service_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResult { + assert($entity instanceof EntityPublishedInterface); // Allow user to view unpublished Errand services based on permission. if ($operation === 'view' && !$entity->isPublished()) { return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_errand_service'); @@ -274,6 +279,7 @@ function helfi_tpr_tpr_errand_service_access(EntityInterface $entity, $operation * Implements hook_query_TAG_alter(). */ function helfi_tpr_query_owd_relationship_alter(AlterableInterface $query) : void { + /** @var \Drupal\Core\Entity\Query\QueryAggregateInterface $query */ $query->groupBy('id'); } diff --git a/helfi_tpr.views_execution.inc b/helfi_tpr.views_execution.inc index c8aa2916..d215b64a 100644 --- a/helfi_tpr.views_execution.inc +++ b/helfi_tpr.views_execution.inc @@ -14,6 +14,7 @@ use Drupal\views\ViewExecutable; */ function helfi_tpr_views_query_alter(ViewExecutable $view, QueryPluginBase $query) { if ($view->id() == 'unit_search' || $view->id() == 'service_units') { + assert(isset($query->orderby)); // Use the CASE function from helfi_tpr_views_pre_execute() for sorting. $query->orderby[0]['field'] = 'name_sort'; $query->orderby[0]['direction'] = 'ASC'; diff --git a/modules/helfi_address_search/src/Plugin/Field/FieldFormatter/KiloFormatter.php b/modules/helfi_address_search/src/Plugin/Field/FieldFormatter/KiloFormatter.php index 2b36d03a..8056f3db 100644 --- a/modules/helfi_address_search/src/Plugin/Field/FieldFormatter/KiloFormatter.php +++ b/modules/helfi_address_search/src/Plugin/Field/FieldFormatter/KiloFormatter.php @@ -45,12 +45,12 @@ public function settingsForm(array $form, FormStateInterface $form_state): array $elements['minimum_value'] = [ '#type' => 'number', - '#title' => t('Minimum value', [], ['context' => 'decimal places']), + '#title' => $this->t('Minimum value', [], ['context' => 'decimal places']), '#step' => '.01', '#min' => 0, '#max' => 1000, '#default_value' => $this->getSetting('minimum_value'), - '#description' => t('If the number is smaller than minimum value, minimum value is shown.'), + '#description' => $this->t('If the number is smaller than minimum value, minimum value is shown.'), '#weight' => 7, ]; diff --git a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php index 99aa804e..4fb9a1a9 100644 --- a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php +++ b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php @@ -6,6 +6,7 @@ use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\Xss; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\views\Plugin\views\filter\FilterPluginBase; @@ -79,6 +80,7 @@ public static function sortByAddress(ViewExecutable $view): ViewExecutable { $results = $view->result; $distances = []; foreach ($results as $result) { + assert($result->_entity instanceof ContentEntityInterface); if (empty($result->_entity->get('latitude')) || empty($result->_entity->get('longitude'))) { continue; } @@ -94,6 +96,7 @@ public static function sortByAddress(ViewExecutable $view): ViewExecutable { // Sort results array by distances: nearest first. uasort($results, function ($left, $right) use ($distances) { + assert($left->_entity instanceof ContentEntityInterface && $right->_entity instanceof ContentEntityInterface); return match ($distances[$left->_entity->get('id')->getString()] >= $distances[$right->_entity->get('id')->getString()]) { FALSE => (-1), TRUE => 1, diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..923eced6 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,27 @@ +parameters: + fileExtensions: + - php + - module + - install + paths: + - ./ + excludePaths: + - vendor + level: 3 + checkMissingIterableValueType: false + treatPhpDocTypesAsCertain: false + ignoreErrors: + - + message: '#^Access to an undefined property#' + paths: + - tests/src/Kernel/UnitMigrationTest.php + - tests/src/Kernel/ServiceMigrationTest.php + - tests/src/Kernel/ServiceChannelMigrationTest.php + - tests/src/Kernel/Plugin/Field/FieldType/ConnectionItemTest.php + - tests/src/Kernel/Plugin/Field/FieldType/AccessibilitySentenceItemTest.php + - tests/src/Functional/UnitListTest.php + - tests/src/Functional/ServiceListTest.php + - + message: '#^\\Drupal calls should be avoided in classes, use dependency injection instead#' + path: src/Entity/TranslationHandler.php + count: 1 diff --git a/src/Entity/ErrandService.php b/src/Entity/ErrandService.php index 412d6875..c282dbf7 100644 --- a/src/Entity/ErrandService.php +++ b/src/Entity/ErrandService.php @@ -148,6 +148,7 @@ public function getChannels() : array { public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); + assert($fields['name'] instanceof BaseFieldDefinition); static::$overrideFields['name'] = $fields['name']; $fields['type'] = static::createStringField('Type'); diff --git a/src/Entity/Form/ContentEntityForm.php b/src/Entity/Form/ContentEntityForm.php index 74687099..ccf4567b 100644 --- a/src/Entity/Form/ContentEntityForm.php +++ b/src/Entity/Form/ContentEntityForm.php @@ -126,16 +126,18 @@ public function form(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function save(array $form, FormStateInterface $form_state) { + public function save(array $form, FormStateInterface $form_state) : int { $entity_type = $this->entity->getEntityTypeId(); - parent::save($form, $form_state); + $status = parent::save($form, $form_state); $this->messenger()->addStatus($this->t('%title saved.', ['%title' => $this->entity->label()])); $form_state->setRedirect('entity.' . $entity_type . '.canonical', [ $entity_type => $this->entity->id(), ]); + + return $status; } } diff --git a/src/Entity/Listing/ListBuilder.php b/src/Entity/Listing/ListBuilder.php index 541f7ec7..3e257712 100644 --- a/src/Entity/Listing/ListBuilder.php +++ b/src/Entity/Listing/ListBuilder.php @@ -15,7 +15,7 @@ /** * Provides a list controller for the tpr entity types. */ -class ListBuilder extends EntityListBuilder { +final class ListBuilder extends EntityListBuilder { /** * The date formatter service. @@ -45,8 +45,8 @@ public function __construct(EntityTypeInterface $entity_type, EntityStorageInter /** * {@inheritdoc} */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { - return new static( + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) : self { + return new self( $entity_type, $container->get('entity_type.manager')->getStorage($entity_type->id()), $container->get('date.formatter'), diff --git a/src/Entity/OntologyWordDetails.php b/src/Entity/OntologyWordDetails.php index a519bdbc..b9da1d9c 100644 --- a/src/Entity/OntologyWordDetails.php +++ b/src/Entity/OntologyWordDetails.php @@ -133,10 +133,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type): a * @param string $langcode * The langcode. * - * @return string[]|null + * @return string[] * Array containing the details. */ - public function getDetailByAnother(string $fieldName, string $detail, string $filterName, string $filterValue, string $langcode): ?array { + public function getDetailByAnother(string $fieldName, string $detail, string $filterName, string $filterValue, string $langcode): array { $data = []; if (!$this->getTranslation($langcode)->get($fieldName)->isEmpty()) { foreach ($this->getTranslation($langcode)->get($fieldName)->getValue() as $item) { @@ -145,7 +145,7 @@ public function getDetailByAnother(string $fieldName, string $detail, string $fi } } } - return $data ?? NULL; + return $data; } } diff --git a/src/Entity/Unit.php b/src/Entity/Unit.php index 0652bdb5..64df8078 100644 --- a/src/Entity/Unit.php +++ b/src/Entity/Unit.php @@ -161,8 +161,8 @@ public function getPictureUrl() : ? string { return $this->get('picture_url')->value; } - /** @var \Drupal\file\FileInterface $file */ if ($file = $picture_url->get('field_media_image')->entity) { + /** @var \Drupal\file\FileInterface $file */ try { return $file->createFileUrl(FALSE) ?: NULL; } diff --git a/src/Plugin/Field/FieldFormatter/ErrandServicesChannelFormatter.php b/src/Plugin/Field/FieldFormatter/ErrandServicesChannelFormatter.php index aaf69a25..2894e5bd 100644 --- a/src/Plugin/Field/FieldFormatter/ErrandServicesChannelFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ErrandServicesChannelFormatter.php @@ -8,9 +8,12 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\helfi_tpr\Entity\ChannelType; use Drupal\helfi_tpr\Entity\ChannelTypeCollection; use Drupal\helfi_tpr\Entity\Service; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Field formatter to render errand service maps. @@ -23,7 +26,29 @@ * } * ) */ -class ErrandServicesChannelFormatter extends FormatterBase { +final class ErrandServicesChannelFormatter extends FormatterBase implements ContainerFactoryPluginInterface { + + /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + private RendererInterface $renderer; + + /** + * {@inheritdoc} + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition + ) : self { + $instance = parent::create($container, $configuration, $plugin_id, + $plugin_definition); + $instance->renderer = $container->get('renderer'); + return $instance; + } /** * {@inheritdoc} @@ -48,10 +73,9 @@ public function settingsSummary() : array { } return [ - $this->t('Showing @list', [ + (string) $this->t('Showing @list', [ '@list' => implode(', ', $selected_channels), - ], - ), + ]), ]; } @@ -132,10 +156,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) : array { } $channelTypes = $this->getChannelTypes(); - /** @var \Drupal\Core\Render\Renderer $renderer */ - $renderer = \Drupal::service('renderer'); - $channel_list = []; + /** @var \Drupal\helfi_tpr\Entity\ErrandService[] $errand_services */ $errand_services = $items->referencedEntities(); $item_list = [ '#theme' => 'item_list', @@ -143,7 +165,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) : array { ]; foreach ($errand_services as $errand_service) { - /** @var \Drupal\helfi_tpr\Entity\ErrandService $errand_service */ foreach ($errand_service->getChannels() as $channel) { if (isset($channel_list[$channel->getType()]) || empty($this->getSetting('sort_order')[$channel->getType()]['show'])) { @@ -154,7 +175,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) : array { '#name' => $this->getSetting('sort_order')[$channel->getType()]['label'], '#weight' => $channelTypes[$channel->getType()]->weight, ]; - $renderer->addCacheableDependency($item_list, $channel); + $this->renderer->addCacheableDependency($item_list, $channel); } } diff --git a/src/Plugin/migrate/source/ServiceMap.php b/src/Plugin/migrate/source/ServiceMap.php index d5d1a92d..74bdfb33 100644 --- a/src/Plugin/migrate/source/ServiceMap.php +++ b/src/Plugin/migrate/source/ServiceMap.php @@ -35,7 +35,7 @@ public function __toString() : string { /** * {@inheritdoc} */ - public function count($refresh = FALSE) { + public function count($refresh = FALSE) : int { return $this->count; } diff --git a/src/Plugin/views/argument/ServiceIdArgument.php b/src/Plugin/views/argument/ServiceIdArgument.php index 3d82c0ba..675481e5 100644 --- a/src/Plugin/views/argument/ServiceIdArgument.php +++ b/src/Plugin/views/argument/ServiceIdArgument.php @@ -3,6 +3,7 @@ namespace Drupal\helfi_tpr\Plugin\views\argument; use Drupal\views\Plugin\views\argument\ArgumentPluginBase; +use Drupal\views\Plugin\views\query\Sql; /** * Argument for service_id column. @@ -30,6 +31,9 @@ public function query($group_by = FALSE) : void { } $group = $this->query->setWhereGroup('OR'); + + assert($this->query instanceof Sql); + if (!empty($ids)) { $this->query->addWhere($group, 'id', $ids, 'IN'); } diff --git a/src/Plugin/views/filter/ProvidedLanguages.php b/src/Plugin/views/filter/ProvidedLanguages.php index de2fb75f..d97e03e5 100644 --- a/src/Plugin/views/filter/ProvidedLanguages.php +++ b/src/Plugin/views/filter/ProvidedLanguages.php @@ -22,7 +22,7 @@ class ProvidedLanguages extends InOperator { */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL): void { parent::init($view, $display, $options); - $this->valueTitle = t('Allowed languages'); + $this->valueTitle = (string) $this->t('Allowed languages'); $this->definition['options callback'] = [$this, 'generateOptions']; } @@ -34,9 +34,9 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o */ public function generateOptions(): array { return [ - 'fi' => t('Finnish'), - 'sv' => t('Swedish'), - 'se' => t('North Sami'), + 'fi' => (string) $this->t('Finnish'), + 'sv' => (string) $this->t('Swedish'), + 'se' => (string) $this->t('North Sami'), ]; } diff --git a/src/Plugin/views/filter/Status.php b/src/Plugin/views/filter/Status.php index 85f1d444..0cf41172 100644 --- a/src/Plugin/views/filter/Status.php +++ b/src/Plugin/views/filter/Status.php @@ -5,6 +5,7 @@ namespace Drupal\helfi_tpr\Plugin\views\filter; use Drupal\views\Plugin\views\filter\FilterPluginBase; +use Drupal\views\Plugin\views\query\Sql; /** * Filter by published status. @@ -28,6 +29,7 @@ public function canExpose() { public function query() { $table = $this->ensureMyTable(); $snippet = "$table.content_translation_status = 1 OR ***VIEW_UNPUBLISHED_TPR_ENTITIES*** = 1"; + assert($this->query instanceof Sql); $this->query->addWhereExpression($this->options['group'], $snippet); } diff --git a/src/Plugin/views/relationship/TagOwdRelationship.php b/src/Plugin/views/relationship/TagOwdRelationship.php index 5bf9f396..234f7c97 100644 --- a/src/Plugin/views/relationship/TagOwdRelationship.php +++ b/src/Plugin/views/relationship/TagOwdRelationship.php @@ -4,6 +4,7 @@ namespace Drupal\helfi_tpr\Plugin\views\relationship; +use Drupal\views\Plugin\views\query\Sql; use Drupal\views\Plugin\views\relationship\RelationshipPluginBase; /** @@ -20,6 +21,7 @@ class TagOwdRelationship extends RelationshipPluginBase { */ public function query() { parent::query(); + assert($this->query instanceof Sql); $this->query->addTag('owd_relationship'); } diff --git a/src/TprViewsData.php b/src/TprViewsData.php index df358c86..dd541ff4 100644 --- a/src/TprViewsData.php +++ b/src/TprViewsData.php @@ -44,14 +44,14 @@ public function getViewsData() { // Add relationship between Unit and Ontology word details. $data['tpr_unit']['tpr_ontology_word_details'] = [ - 'title' => t('Ontology word details field data'), + 'title' => $this->t('Ontology word details field data'), 'relationship' => [ 'base' => 'tpr_ontology_word_details_field_data', 'base field' => 'unit_id', 'table' => 'tpr_unit', 'real field' => 'id', 'id' => 'tag_owd_relationship', - 'label' => t('Ontology word details field data'), + 'label' => $this->t('Ontology word details field data'), ], ]; diff --git a/tests/src/Functional/ErrandServiceListTest.php b/tests/src/Functional/ErrandServiceListTest.php index 5b117b27..cb93a54b 100644 --- a/tests/src/Functional/ErrandServiceListTest.php +++ b/tests/src/Functional/ErrandServiceListTest.php @@ -5,6 +5,8 @@ namespace Drupal\Tests\helfi_tpr\Functional; use donatj\MockWebServer\Response; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\TranslatableInterface; use Drupal\helfi_tpr\Entity\ErrandService; /** @@ -36,7 +38,10 @@ public function setUp() : void { * {@inheritdoc} */ protected function populateMockQueue(): void { - foreach ($this->fixture($this->entityType)->getMockData() as $item) { + /** @var \Drupal\helfi_tpr\Fixture\ErrandService $fixture */ + $fixture = $this->fixture($this->entityType); + + foreach ($fixture->getMockData() as $item) { $url = sprintf('/%s/%s', $this->entityType, $item['id']); $this->webServer ->setResponseOfPath($url, new Response(json_encode($item['fi']))); @@ -118,13 +123,19 @@ public function testList() : void { $this->assertUpdateAction(['fi']); $storage = \Drupal::entityTypeManager()->getStorage('tpr_errand_service'); - $items = $this->fixture('tpr_errand_service')->getMockData(); + /** @var \Drupal\helfi_tpr\Fixture\ErrandService $fixture */ + $fixture = $this->fixture($this->entityType); + $items = $fixture->getMockData(); // Make sure entity data is updated back to normal. foreach ($items as $item) { $item = $item['fi']; $storage->resetCache([$item['id']]); - $entity = $storage->load($item['id'])->getTranslation('fi'); + $entity = $storage->load($item['id']); + assert($entity instanceof TranslatableInterface); + assert($entity instanceof ContentEntityInterface); + + $entity = $entity->getTranslation('fi'); $this->assertEquals($item['name'], $entity->label()); $this->assertNotEquals(sprintf('Description %s fi', $item['id']), $entity->get('description')->value); diff --git a/tests/src/Functional/ServiceListTest.php b/tests/src/Functional/ServiceListTest.php index a189030f..3e9d2d68 100644 --- a/tests/src/Functional/ServiceListTest.php +++ b/tests/src/Functional/ServiceListTest.php @@ -5,6 +5,8 @@ namespace Drupal\Tests\helfi_tpr\Functional; use donatj\MockWebServer\Response; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\TranslatableInterface; use Drupal\helfi_tpr\Entity\Service; /** @@ -35,7 +37,10 @@ public function setUp() : void { * {@inheritdoc} */ protected function populateMockQueue(): void { - foreach ($this->fixture($this->entityType)->getMockData() as $item) { + /** @var \Drupal\helfi_tpr\Fixture\Service $fixture */ + $fixture = $this->fixture($this->entityType); + + foreach ($fixture->getMockData() as $item) { $url = sprintf('/%s/%s', $this->entityType, $item['fi']['id']); $this->webServer ->setResponseOfPath($url, new Response(json_encode($item['fi']))); @@ -124,12 +129,19 @@ public function testList() : void { $this->assertUpdateAction(['fi']); $storage = \Drupal::entityTypeManager()->getStorage($this->entityType); - $items = $this->fixture($this->entityType)->getMockData(); + /** @var \Drupal\helfi_tpr\Fixture\Service $fixture */ + $fixture = $this->fixture($this->entityType); + $items = $fixture->getMockData(); + // Make sure service data is updated back to normal. foreach ($items as $item) { $item = $item['fi']; $storage->resetCache([$item['id']]); - $entity = $storage->load($item['id'])->getTranslation('fi'); + $entity = $storage->load($item['id']); + assert($entity instanceof TranslatableInterface); + assert($entity instanceof ContentEntityInterface); + + $entity = $entity->getTranslation('fi'); $this->assertEquals($item['title'], $entity->label()); $this->assertEquals(count($item['exact_errand_services']), $entity->get('errand_services')->count()); diff --git a/tests/src/Functional/UnitListTest.php b/tests/src/Functional/UnitListTest.php index 007cc8ad..f33a3d13 100644 --- a/tests/src/Functional/UnitListTest.php +++ b/tests/src/Functional/UnitListTest.php @@ -5,6 +5,8 @@ namespace Drupal\Tests\helfi_tpr\Functional; use donatj\MockWebServer\Response; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\TranslatableInterface; use Drupal\helfi_tpr\Entity\Unit; /** @@ -35,7 +37,10 @@ public function setUp() : void { * {@inheritdoc} */ protected function populateMockQueue(): void { - foreach ($this->fixture($this->entityType)->getMockData() as $item) { + /** @var \Drupal\helfi_tpr\Fixture\Unit $fixture */ + $fixture = $this->fixture($this->entityType); + + foreach ($fixture->getMockData() as $item) { $url = sprintf('/%s/%s', $this->entityType, $item['id']); $this->webServer ->setResponseOfPath($url, new Response(json_encode($item))); @@ -124,12 +129,18 @@ public function testList() : void { $this->assertUpdateAction(['fi']); $storage = \Drupal::entityTypeManager()->getStorage($this->entityType); - $items = $this->fixture($this->entityType)->getMockData(); + /** @var \Drupal\helfi_tpr\Fixture\Unit $fixture */ + $fixture = $this->fixture($this->entityType); + $items = $fixture->getMockData(); // Make sure entity data is updated back to normal. foreach ($items as $item) { $storage->resetCache([$item['id']]); - $entity = $storage->load($item['id'])->getTranslation('fi'); + $entity = $storage->load($item['id']); + assert($entity instanceof TranslatableInterface); + assert($entity instanceof ContentEntityInterface); + + $entity = $entity->getTranslation('fi'); $this->assertEquals($item['name_fi'], $entity->label()); $this->assertNotEquals(sprintf('Description %s fi', $item['id']), $entity->get('description')->value); diff --git a/tests/src/Kernel/ErrandServiceMigrationTest.php b/tests/src/Kernel/ErrandServiceMigrationTest.php index 0929bcec..a38de472 100644 --- a/tests/src/Kernel/ErrandServiceMigrationTest.php +++ b/tests/src/Kernel/ErrandServiceMigrationTest.php @@ -60,7 +60,9 @@ public function testErrandServiceMigration() : void { $this->assertEquals($langcode, $translation->language()->getId()); - foreach ($this->fixture('tpr_errand_service')->getFields() as $field) { + /** @var \Drupal\helfi_tpr\Fixture\ErrandService $fixture */ + $fixture = $this->fixture('tpr_errand_service'); + foreach ($fixture->getFields() as $field) { $this->assertEquals( sprintf('%s %s %s', $field, $langcode, $translation->id()), $translation->get($field)->value diff --git a/tests/src/Kernel/OverrideFieldTest.php b/tests/src/Kernel/OverrideFieldTest.php index 39596bed..7447db56 100644 --- a/tests/src/Kernel/OverrideFieldTest.php +++ b/tests/src/Kernel/OverrideFieldTest.php @@ -4,6 +4,8 @@ namespace Drupal\Tests\helfi_tpr\Kernel; +use Drupal\Core\Entity\ContentEntityInterface; + /** * Tests override fields. * @@ -18,6 +20,7 @@ class OverrideFieldTest extends MigrationTestBase { */ public function testName(string $entity_type) : void { $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create(['id' => 1]); + assert($entity instanceof ContentEntityInterface); $entity->set('name', 'Name no override') ->save(); $this->assertEquals('Name no override', $entity->label());