class DefaultContentTest

Tests exporting menu links in YAML format.

Attributes

#[Group('menu_link_content')] #[CoversClass(DefaultContentSubscriber::class)] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of DefaultContentTest

File

core/modules/menu_link_content/tests/src/Kernel/DefaultContentTest.php, line 24

Namespace

Drupal\Tests\menu_link_content\Kernel
View source
class DefaultContentTest extends KernelTestBase {
  use ContentTypeCreationTrait;
  use NodeCreationTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field',
    'filter',
    'link',
    'menu_link_content',
    'node',
    'system',
    'text',
    'user',
  ];
  
  /**
   * Tests exporting of menu link content.
   */
  public function testExportMenuLinkContent() : void {
    $this->installConfig([
      'filter',
      'system',
    ]);
    $this->installEntitySchema('menu_link_content');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->createContentType([
      'type' => 'page',
    ]);
    $parent = $this->createNode([
      'type' => 'page',
    ]);
    $child = $this->createNode([
      'type' => 'page',
    ]);
    \Drupal::service(MenuLinkManagerInterface::class)->rebuild();
    $parent_link = MenuLinkContent::create([
      'menu_name' => 'main',
      'link' => 'internal:' . $parent->toUrl()
        ->toString(),
    ]);
    $parent_link->save();
    $child_link = MenuLinkContent::create([
      'menu_name' => 'main',
      'link' => 'internal:' . $child->toUrl()
        ->toString(),
      'parent' => 'menu_link_content:' . $parent_link->uuid(),
    ]);
    $child_link->save();
    $logger = new TestLogger();
    \Drupal::service('logger.channel.default_content')->addLogger($logger);
    // If we export the child link, the parent should be one of its
    // dependencies.
    $data = (string) \Drupal::service(Exporter::class)->export($child_link);
    $data = Yaml::decode($data);
    $this->assertArrayHasKey($parent_link->uuid(), $data['_meta']['depends']);
    $this->assertEmpty($logger->records);
    // If we delete the parent link, exporting the child should log an error.
    $parent_link->delete();
    \Drupal::service(Exporter::class)->export($child_link);
    $predicate = function (array $record) use ($child_link, $parent_link) : bool {
      return $record['message'] === 'The parent (%parent) of menu link %uuid could not be loaded.' && $record['context']['%parent'] === $parent_link->uuid() && $record['context']['%uuid'] === $child_link->uuid();
    };
    $this->assertTrue($logger->hasRecordThatPasses($predicate, RfcLogLevel::ERROR));
  }

}

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