MenuValidationTest.php

Namespace

Drupal\Tests\system\Kernel\Entity

File

core/modules/system/tests/src/Kernel/Entity/MenuValidationTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\system\Kernel\Entity;

use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;
use Drupal\system\Entity\Menu;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\Attributes\TestWith;

/**
 * Tests validation of menu entities.
 */
class MenuValidationTest extends ConfigEntityValidationTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static array $propertiesWithOptionalValues = [
    'description',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entity = Menu::create([
      'id' => 'test-menu',
      'label' => 'Test',
    ]);
    $this->entity
      ->save();
  }
  
  /**
   * Menu IDs are atypical: they allow dashes and disallow underscores.
   */
  public static function providerInvalidMachineNameCharacters() : array {
    $cases = parent::providerInvalidMachineNameCharacters();
    // Remove the existing test case that verifies a machine name containing
    // dashes is invalid.
    self::assertSame([
      'dash-separated',
      FALSE,
    ], $cases['INVALID: dash separated']);
    unset($cases['INVALID: dash separated']);
    // And instead add a test case that verifies it is allowed for menus.
    $cases['VALID: dash separated'] = [
      'dash-separated',
      TRUE,
    ];
    // Remove the existing test case that verifies a machine name containing
    // underscores is valid.
    self::assertSame([
      'underscore_separated',
      TRUE,
    ], $cases['VALID: underscore separated']);
    unset($cases['VALID: underscore separated']);
    // And instead add a test case that verifies it is disallowed for menus.
    $cases['INVALID: underscore separated'] = [
      'underscore_separated',
      FALSE,
    ];
    return $cases;
  }
  
  /**
   * Tests that description is optional, and limited to 512 characters.
   *
   * phpcs:ignore Drupal.Commenting.DocComment.LongNotCapital
   * cspell:disable
   */
  public function testDescription(?string $description, array $expected_errors) : void {
    // cspell:enable
    $this->entity
      ->set('description', $description);
    $this->assertValidationErrors($expected_errors);
  }

}

Classes

Title Deprecated Summary
MenuValidationTest Tests validation of menu entities.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.