reviewer-1.2.x-dev/modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldPluginsTaskBaseTest.php
modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldPluginsTaskBaseTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\reviewer_test_kit\Kernel\Entity\Display;
use Drupal\reviewer\Reviewer\Action;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\Form\FieldWidgetsTaskBase;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\View\FieldFormattersTaskBase;
use Drupal\Tests\reviewer_test_kit\Kernel\Entity\EntityTaskBaseTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
/**
* Test for tasks which check field plugins on entity display forms.
*/
#[Group('reviewer')]
#[CoversClass(FieldWidgetsTaskBase::class)]
#[CoversClass(FieldFormattersTaskBase::class)]
final class FieldPluginsTaskBaseTest extends EntityTaskBaseTestBase {
/**
* Test the checking and fixing of field widget plugins.
*
* @throws \Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
*/
public function testFieldWidgetPlugins(): void {
$this
->loadForm()
->setComponent('field_node', [
'type' => 'options_select',
'region' => 'content',
])
->save();
// Test that check returns the expected errors.
$result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_field_widgets_review');
self::assertSame('Field "field_node" plugin must be one of: "entity_reference_autocomplete", "entity_reference_autocomplete_tags".', $result->getMessage());
// Test that fix is unable to proceed because there are multiple allowed
// plugins.
$result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_field_widgets_review');
self::assertSame('Cannot set field plugins on "node.reviewer" because there is more than one allowed plugin ("entity_reference_autocomplete", "entity_reference_autocomplete_tags").', $result->getMessage());
}
/**
* Test the checking and fixing of field formatter plugins.
*
* @throws \Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
*/
public function testFieldFormatterPlugins(): void {
$this
->loadView()
->setComponent('field_node', [
'type' => 'entity_reference_entity_id',
'region' => 'content',
])
->save();
// Test that check returns the expected errors.
$result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_field_formatters_review');
self::assertSame('Field "field_node" plugin must be one of: "entity_reference_label".', $result->getMessage());
// Test that fix returns the correct value.
$result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_field_formatters_review');
self::assertSame('Fixed field plugins for "node.reviewer".', $result->getMessage());
// Test that the fix sets the configuration correctly.
self::assertSame('entity_reference_label', $this->loadView()->getComponent('field_node')['type'] ?? '');
}
}
