reviewer-1.2.x-dev/modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldSettingsTaskBaseTest.php

modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldSettingsTaskBaseTest.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\FieldWidgetSettingsTaskBase;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\View\FieldFormatterSettingsTaskBase;
use Drupal\Tests\reviewer_test_kit\Kernel\Entity\EntityTaskBaseTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Test for tasks which checks field settings on entity display forms.
 */
#[Group('reviewer')]
#[CoversClass(FieldWidgetSettingsTaskBase::class)]
#[CoversClass(FieldFormatterSettingsTaskBase::class)]
final class FieldSettingsTaskBaseTest extends EntityTaskBaseTestBase {

  /**
   * Test the checking and fixing of entity form display field settings.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testFieldWidgetSettings(): void {
    $this
      ->loadForm()
      ->setComponent('body', [
        'weight' => 0,
        'settings' => [
          'rows' => 9,
          'summary_rows' => 3,
          'placeholder' => '',
        ],
      ])
      ->save();

    // Test that the correct errors are returned.
    $result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_field_widget_settings_review');
    self::assertSame('Settings for field "node.reviewer.default.body.weight", "node.reviewer.default.body.settings.summary_rows", "node.reviewer.default.body.settings.placeholder" are incorrect.', $result->getMessage());

    // Test that fix is not able to handle a negated value.
    $result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_field_widget_settings_review');
    self::assertSame('Unable to fix field setting "node.reviewer.default.body.settings.placeholder" due to negated values.', $result->getMessage());
    // Test that the fix sets the configuration correctly. Only check the
    // configuration set above.
    $settings = array_filter(
      $this->loadForm()->getComponent('body') ?? [],
      fn(string $key): bool => \in_array($key, ['weight', 'settings'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'weight' => -10,
        'settings' => [
          'rows' => 9,
          'summary_rows' => 5,
          'placeholder' => '',
          'show_summary' => FALSE,
        ],
      ],
      $settings,
    );

    // Set a placeholder value that satisfies the negation.
    $this
      ->loadForm()
      ->setComponent('title', [
        'weight' => 0,
        'settings' => [
          'rows' => 9,
          'summary_rows' => 3,
          'placeholder' => 'Placeholder',
          'show_summary' => FALSE,
        ],
      ])
      ->save();
    // Test that the negation is checked correctly.
    $result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_field_widget_settings_review');
    self::assertSame('Settings for field "node.reviewer.default.body.settings.placeholder" are incorrect.', $result->getMessage());
  }

  /**
   * Test the checking and fixing of entity view display field settings.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testFieldFormatterSettings(): void {
    $this
      ->loadView()
      ->setComponent('body', [
        'type' => 'text_trimmed',
        'label' => 'above',
      ])
      ->save();

    // Test that the correct errors are returned.
    $result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_field_formatter_settings_review');
    self::assertSame('Settings for field "node.reviewer.default.body.label", "node.reviewer.default.body.type" are incorrect.', $result->getMessage());

    // Test that fixes are applied correctly.
    $result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_field_formatter_settings_review');
    self::assertSame('Fixed field settings for "node.reviewer".', $result->getMessage());
    // Test that the fix sets the configuration correctly. Only check the
    // configuration set above.
    $settings = array_filter(
      $this->loadView()->getComponent('body') ?? [],
      fn(string $key): bool => \in_array($key, ['label', 'type'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'type' => 'text_default',
        'label' => 'hidden',
      ],
      $settings,
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc