reviewer-1.2.x-dev/modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldsDisabledTaskBaseTest.php
modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldsDisabledTaskBaseTest.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\View\ViewFieldsDisabledTaskBase;
use Drupal\Tests\reviewer_test_kit\Kernel\Entity\EntityTaskBaseTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
/**
* Test for tasks which check if fields are disabled on entity display forms.
*/
#[Group('reviewer')]
#[CoversClass(ViewFieldsDisabledTaskBase::class)]
#[CoversClass(ViewFieldsDisabledTaskBase::class)]
final class FieldsDisabledTaskBaseTest extends EntityTaskBaseTestBase {
/**
* Test the checking and fixing of entity form display form fields.
*
* @throws \Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
*/
public function testFormFieldsDisabled(): void {
// Set incorrect form values.
$this
->loadForm()
->setComponent('created', [
'type' => 'datetime_timestamp',
'region' => 'content',
])
->setComponent('sticky', [
'type' => 'boolean_checkbox',
'region' => 'content',
])
->setComponent('field_node', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
])
->save();
// Test the initial state.
self::assertSame([], $this->loadForm()->get('hidden'));
// Test that check returns the expected errors.
$review = $this->runOneTaskReview(Action::Check, 'test_form_fields_disabled_review');
self::assertNotCount(0, $review->getResults()->getIndividualResults());
foreach ($review->getResults()->getIndividualResults() as $result) {
$field = array_slice(explode('.', $result->getId()), -1)[0] ?? '';
self::assertSame("Field $field is not disabled.", $result->getMessage());
}
// Test that fix returns the correct value.
$result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_form_fields_disabled_review');
self::assertSame('Disabled fields for node.reviewer.', $result->getMessage());
// Test that the fix sets the configuration correctly.
self::assertSame(['created' => TRUE, 'field_node' => TRUE, 'sticky' => TRUE], $this->loadForm()->get('hidden'));
}
/**
* Test the checking and fixing of entity view display form fields.
*
* @throws \Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
*/
public function testViewFieldsDisabled(): void {
// Set incorrect form values.
$this
->loadView()
->setComponent('body', [
'type' => 'text_default',
'region' => 'content',
])
->setComponent('links', [
'region' => 'content',
])
->setComponent('field_node', [
'type' => 'entity_reference_label',
'region' => 'content',
])
->save();
// The langcode field is hidden by default, so add it as a base hidden item.
$base_hidden = ['langcode' => TRUE];
// Test the initial state.
self::assertSame($base_hidden, $this->loadView()->get('hidden'));
// Test that check returns the expected errors.
$review = $this->runOneTaskReview(Action::Check, 'test_view_fields_disabled_review');
self::assertNotCount(0, $review->getResults()->getIndividualResults());
foreach ($review->getResults()->getIndividualResults() as $result) {
$field = array_slice(explode('.', $result->getId()), -1)[0] ?? '';
self::assertSame("Field $field is not disabled.", $result->getMessage());
}
// Test that fix returns the correct value.
$result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_view_fields_disabled_review');
self::assertSame('Disabled fields for node.reviewer.', $result->getMessage());
// Test that the fix sets the configuration correctly.
self::assertSame(['body' => TRUE, 'field_node' => TRUE, 'links' => TRUE] + $base_hidden, $this->loadView()->get('hidden'));
}
}
