<?php

declare(strict_types=1);

namespace Drupal\Tests\node\Functional\Views;

use Drupal\node\Entity\Node;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests replacement of Views tokens supplied by the Node module.
 *
 * @see \Drupal\node\Tests\NodeTokenReplaceTest
 */
#[Group('node')]
#[RunTestsInSeparateProcesses]
class NodeFieldTokensTest extends NodeTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = ['test_node_tokens'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests token replacement for Views tokens supplied by the Node module.
   */
  public function testViewsTokenReplacement(): void {
    // Create the Article content type with a standard body field.
    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);

    // Create a user and a node.
    $account = $this->createUser();
    $body = $this->randomMachineName(32);

    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
      'type' => 'article',
      'uid' => $account->id(),
      'title' => 'Testing Views tokens',
      'body' => [['value' => $body, 'format' => 'plain_text']],
    ]);
    $node->save();

    $this->drupalGet('test_node_tokens');

    // Body: "{{ body }}<br />".
    $this->assertSession()->responseContains("Body: <p>$body</p>");

    // Raw value: "{{ body__value }}<br />".
    $this->assertSession()->responseContains("Raw value: $body");

    // Raw format: "{{ body__format }}<br />".
    $this->assertSession()->responseContains("Raw format: plain_text");
  }

}
