<?php

namespace Drupal\metatag_page_manager\Tests\Functional;

use Drupal\page_manager\Entity\Page;
use Drupal\page_manager\Entity\PageVariant;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\metatag\Functional\MetatagHelperTrait;

/**
 * Confirm the Page Manager integration works.
 *
 * @group metatag
 */
class MetatagPageManagerTest extends BrowserTestBase {

  use MetatagHelperTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    // This module.
    'metatag_page_manager',
  ];

  /**
   * The assert session object.
   *
   * @var \Drupal\Tests\WebAssert
   */
  public $assertSession;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    // TODO: Change the autogenerated stub.
    parent::setUp();

    $this->assertSession = $this->assertSession();

    Page::create([
      'id' => 'metatag_page_manager_test',
      'label' => 'Metatag Page',
      'path' => '/metatag-test',
    ])->save();
    PageVariant::create([
      'id' => 'metatag_page_manager_variant_test',
      'variant' => 'block_display',
      'label' => 'Metatag Variant',
      'page' => 'metatag_page_manager_test',
      'weight' => 10,
    ])->save();

    \Drupal::service("router.builder")->rebuild();

    // Log in as user 1.
    $this->loginUser1();
  }

  /**
   * Tests a single variant page.
   */
  public function testSingleVariantPage() {
    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);

    // Confirm what the page title looks like by default.
    $this->assertSession->titleEquals('Metatag Page | Drupal');

    // Create the Metatag object through the UI to check the custom label.
    $edit = [
      'id' => 'page_variant__metatag_page_manager_variant_test',
      'title' => 'My title',
    ];

    $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
    $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');

    // Clear caches to load the right metatags.
    drupal_flush_all_caches();

    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);

    // Confirm what the page title is overridden.
    $this->assertSession->titleEquals('My title');
  }

  /**
   * Tests a single variant page.
   */
  public function testMultipleVariantPage() {
    // Add a new variant.
    $new_variant = PageVariant::create([
      'id' => 'metatag_page_manager_multiple_variant_test',
      'variant' => 'block_display',
      'label' => 'Metatag Multiple Variant',
      'page' => 'metatag_page_manager_test',
      'weight' => 0,
    ]);
    $anonymous_selection = [
      'id' => 'user_role',
      'roles' => [
        'anonymous' => 'anonymous',
      ],
      'negate' => FALSE,
      'context_mapping' => [
        'user' => 'current_user',
      ],
    ];
    $new_variant->set('selection_criteria', [$anonymous_selection]);
    $new_variant->save();

    // Clear caches to load the right metatags.
    drupal_flush_all_caches();

    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);

    // Confirm what the page title looks like by default.
    $this->assertSession->titleEquals('Metatag Page | Drupal');

    // Create the Metatag object through the UI to check the custom label.
    $edit = [
      'id' => 'page_variant__metatag_page_manager_variant_test',
      'title' => 'My title',
    ];

    $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
    $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');

    // Clear caches to load the right metatags.
    drupal_flush_all_caches();

    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);

    // Confirm what the page title is overridden.
    $this->assertSession->titleEquals('My title');

    // Visiting page as anon user, should get the default title.
    $this->drupalLogout();
    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);

    // Confirm what the page title looks like by default.
    $this->assertSession->titleEquals('Metatag Page | Drupal');

    // Login and add custom metatag for anonymous user variant.
    $this->loginUser1();
    // Create the Metatag object through the UI to check the custom label.
    $edit = [
      'id' => 'page_variant__metatag_page_manager_multiple_variant_test',
      'title' => 'My title anonymous',
    ];

    $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
    $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Multiple Variant');

    // Clear caches to load the right metatags.
    drupal_flush_all_caches();

    // Visit page as logged in user and confirm the right title.
    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);
    $this->assertSession->titleEquals('My title');

    // Visit page as anonymous user and confirm the right title.
    $this->drupalLogout();
    $this->drupalGet('/metatag-test');
    $this->assertSession->statusCodeEquals(200);
    $this->assertSession->titleEquals('My title anonymous');
  }

}
