Skip to content

Commit

Permalink
Merge pull request #644 from City-of-Helsinki/UHF-9977-tests
Browse files Browse the repository at this point in the history
UHF-9977: Add tests
  • Loading branch information
hyrsky authored Jun 12, 2024
2 parents ccbc93c + 646b516 commit 0cb2bdc
Show file tree
Hide file tree
Showing 12 changed files with 847 additions and 112 deletions.
92 changes: 46 additions & 46 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion public/modules/custom/helfi_annif/helfi_annif.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ type: module
core_version_requirement: ^10
dependencies:
- drupal:taxonomy
- drupal:path
- drupal:text
- drupal:language
- helfi_api_base:helfi_api_base
- elasticsearch_connector:elasticsearch_connector
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Error;
use Drupal\helfi_annif\Client\KeywordClient;
use Drupal\helfi_annif\KeywordManager;
use Drupal\helfi_annif\TextConverter\TextConverterManager;
Expand Down Expand Up @@ -154,7 +155,7 @@ public function processBatch(
/**
* Preview entity text conversion result.
*
* @param string $entityType
* @param string $entity_type
* The entity type.
* @param string $id
* The entity id.
Expand All @@ -170,12 +171,17 @@ public function processBatch(
#[Option(name: 'language', description: 'Entity language', suggestedValues: ['fi', 'sv', 'en'])]
#[Usage(name: 'drush helfi:preview-text node 123', description: 'Preview node with id 123.')]
#[Usage(name: 'drush helfi:preview-text node 123 --language sv', description: 'Preview swedish translation of node 123.')]
public function preview(string $entityType, string $id, array $options = ['language' => NULL]) : int {
public function preview(string $entity_type, string $id, array $options = ['language' => NULL]) : int {
try {
$entity = $this->entityTypeManager
->getStorage($entityType)
->getStorage($entity_type)
->load($id);

if (!$entity) {
$this->io->error("Failed to load $entity_type:$id");
return self::EXIT_FAILURE;
}

if (
!empty($options['language']) &&
$entity instanceof TranslatableInterface &&
Expand All @@ -184,13 +190,17 @@ public function preview(string $entityType, string $id, array $options = ['langu
$entity = $entity->getTranslation($options['language']);
}

if ($entity && $content = $this->textConverter->convert($entity)) {
if ($content = $this->textConverter->convert($entity)) {
$this->io()->text($content);

return DrushCommands::EXIT_SUCCESS;
}
else {
$this->io()->error("Failed to find text converter for $entity_type:$id");
}
}
catch (InvalidPluginDefinitionException | PluginNotFoundException) {
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
Error::logException($this->logger(), $e);
}

return DrushCommands::EXIT_FAILURE;
Expand Down
Loading

0 comments on commit 0cb2bdc

Please sign in to comment.