Skip to content
Snippets Groups Projects
Commit 95120f2e authored by Dmitry Korkhau's avatar Dmitry Korkhau Committed by Dmitry Korkhau
Browse files

Issue #3341065 by dmitry.korhov: Create Kernel test with fake custom CreditParser plugin

parent 9e92af14
No related branches found
No related tags found
1 merge request!9Issue #3341065 - added kernel tests, copied annotation & abstract class fixes
<?php
namespace Drupal\dosd\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Dosd annotation object.
*
* Additional annotation keys for formatters can be defined in
* hook_field_formatter_info_alter().
*
* @see \Drupal\Core\Field\FormatterPluginManager
* @see \Drupal\Core\Field\FormatterInterface
*
* @ingroup field_formatter
*
* @Annotation
*/
class CreditParser extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the formatter type.
*
* @var \Drupal\Core\Annotation\Translation
* @ingroup plugin_translatable
*/
public $label;
}
......@@ -61,7 +61,7 @@ class CreditParsersManager extends DefaultPluginManager {
foreach (array_keys($this->getDefinitions()) as $plugin_id) {
/** @var \Drupal\dosd\CreditParserPluginInterface $plugin */
$plugin = $this->createInstance($plugin_id);
array_push($credits, $plugin->getCredits());
$credits = array_merge($credits, $plugin->getCredits());
}
foreach ($credits as &$credit) {
/** @var \Drupal\dosd\Entity\CreditEntityInterface $credit */
......
......@@ -17,6 +17,21 @@ abstract class CreditParserBase extends PluginBase implements CreditParserPlugin
/**
* {@inheritdoc}
*/
public function getCredits() {}
abstract public function getCredits(): array;
/**
* {@inheritdoc}
*/
abstract public function getCacheContexts();
/**
* {@inheritdoc}
*/
abstract public function getCacheTags();
/**
* {@inheritdoc}
*/
abstract public function getCacheMaxAge();
}
name: Test module for Drupal Open Source Dashboard
type: module
description: 'Provides test data'
core_version_requirement: ^9 || ^10
<?php
namespace Drupal\dosd_test\Plugin\dosd\CreditParser;
use Drupal\dosd\Entity\CreditEntity;
use Drupal\dosd\Plugin\dosd\CreditParser\CreditParserBase;
/**
* Provides a 'FakeCreditParser' plugin.
*
* @CreditParser(
* id = "FakeCreditParser",
* label = @Translation("FakeCreditParser"),
* )
*/
class FakeCreditParser extends CreditParserBase {
/**
* {@inheritdoc}
*/
public function getCredits(): array {
$fake_credit = CreditEntity::create([
'link' => 'https://drupal.org/i/fakeid',
'uid' => '1',
]);
return [$fake_credit];
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return [];
}
}
<?php
namespace Drupal\Tests\dosd\Kernel;
use Drupal\dosd\Entity\CreditEntity;
use Drupal\KernelTests\KernelTestBase;
/**
* Test for plugin.manager.dosd.parser.
*
* @coversDefaultClass \Drupal\dosd\CreditParsersManager
*/
class CreditParserManagerTest extends KernelTestBase {
/**
* The service under test.
*
* @var \Drupal\dosd\CreditParsersManager
*/
protected $parserManager;
/**
* The modules to load to run the test.
*
* @var array
*/
public static $modules = ['dosd', 'dosd_test', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('credit_entity');
$this->installEntitySchema('user');
$this->parserManager = \Drupal::service('plugin.manager.dosd.parser');
}
/**
* @covers ::collectCredits
*/
public function testCollectCredits() {
$this->parserManager->collectCredits();
$credits = CreditEntity::loadMultiple();
foreach ($credits as $credit) {
$link = $credit->get('link')->getString();
$this->assertSame('https://drupal.org/i/fakeid', $link);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment