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

modules/reviewer_test_kit/tests/src/Kernel/Entity/Display/FieldGroupSettingsTaskBaseTest.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\FormFieldGroupSettingsTaskBase;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\View\ViewFieldGroupSettingsTaskBase;
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 group settings on entity display forms.
 */
#[Group('reviewer')]
#[CoversClass(FormFieldGroupSettingsTaskBase::class)]
#[CoversClass(ViewFieldGroupSettingsTaskBase::class)]
final class FieldGroupSettingsTaskBaseTest extends EntityTaskBaseTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field_group',
  ];

  /**
   * Test the checking and fixing of entity form display field groups.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testFormFieldGroups(): void {
    $this
      ->loadForm()
      ->setThirdPartySetting('field_group', 'test_one', [
        'format_type' => 'details',
        'format_settings' => [
          'open' => TRUE,
          'required_fields' => TRUE,
        ],
      ])
      ->setThirdPartySetting('field_group', 'test_two', [
        'format_type' => 'details_sidebar',
        'format_settings' => [
          'label_as_html' => TRUE,
          'open' => TRUE,
          'required_fields' => TRUE,
        ],
      ])
      ->save();

    // Test that check returns the expected errors.
    $review = $this->runOneTaskReview(Action::Check, 'test_form_field_group_settings_review');
    self::assertNotCount(0, $review->getResults()->getIndividualResults());
    foreach ($review->getResults()->getIndividualResults() as $result) {
      self::assertStringContainsString('incorrect.', $result->getMessage());
    }

    // Test that fix returns the correct value.
    $result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_form_field_group_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.
    $one_settings = $this->loadForm()->getThirdPartySetting('field_group', 'test_one', []);
    self::assertIsArray($one_settings);
    $one_settings = array_filter(
      $one_settings,
      fn(string $key): bool => \in_array($key, ['format_type', 'format_settings'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'format_type' => 'details',
        'format_settings' => [
          'open' => FALSE,
          'required_fields' => FALSE,
        ],
      ],
      $one_settings,
    );
    $two_settings = $this->loadForm()->getThirdPartySetting('field_group', 'test_two', []);
    self::assertIsArray($two_settings);
    $two_settings = array_filter(
      $two_settings,
      fn(string $key): bool => \in_array($key, ['format_type', 'format_settings'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'format_type' => 'details',
        'format_settings' => [
          'label_as_html' => TRUE,
          'open' => FALSE,
          'required_fields' => FALSE,
        ],
      ],
      $two_settings,
    );
  }

  /**
   * Test the checking and fixing of entity view display field groups.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testViewFieldGroups(): void {
    $this
      ->loadView()
      ->setThirdPartySetting('field_group', 'test_one', [
        'format_type' => 'details_sidebar',
        'format_settings' => [
          'open' => TRUE,
        ],
      ])
      ->setThirdPartySetting('field_group', 'test_two', [
        'format_type' => 'details_sidebar',
        'format_settings' => [],
      ])
      ->save();

    // Test that check returns the expected errors.
    $review = $this->runOneTaskReview(Action::Check, 'test_view_field_group_settings_review');
    self::assertNotCount(0, $review->getResults()->getIndividualResults());
    foreach ($review->getResults()->getIndividualResults() as $result) {
      self::assertStringContainsString('incorrect.', $result->getMessage());
    }

    // Test that fix returns the correct value.
    $result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_view_field_group_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.
    $one_settings = $this->loadView()->getThirdPartySetting('field_group', 'test_one', []);
    self::assertIsArray($one_settings);
    $one_settings = array_filter(
      $one_settings,
      fn(string $key): bool => \in_array($key, ['format_type', 'format_settings'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'format_type' => 'details',
        'format_settings' => [
          'open' => FALSE,
          'required_fields' => FALSE,
        ],
      ],
      $one_settings,
    );
    $two_settings = $this->loadView()->getThirdPartySetting('field_group', 'test_two', []);
    self::assertIsArray($two_settings);
    $two_settings = array_filter(
      $two_settings,
      fn(string $key): bool => \in_array($key, ['format_type', 'format_settings'], TRUE),
      ARRAY_FILTER_USE_KEY,
    );
    self::assertSame(
      [
        'format_type' => 'details',
        'format_settings' => [
          'open' => FALSE,
          'required_fields' => FALSE,
        ],
      ],
      $two_settings,
    );
  }

}

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

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