<?php


namespace Drupal\FunctionalTests\Core\Entity;

use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests entity translation functionality related to publishing.
 *
 * @group Entity
 */
class EntityTranslationPublishTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'node',
    'content_translation',
    'entity_test',
    'language',
  ];

  /**
   * Switch to the multilingual testing profile.
   *
   * @var string
   */
  protected $profile = 'testing_multilingual';

  /**
   * {@inheritdoc}
   */
  protected function setUpLanguage() {
    ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('es')->save();
    parent::setUpLanguage();
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->contentType = 'page';
    $this->drupalCreateContentType(['type' => $this->contentType, 'name' => 'Basic page']);
    $this->node1 = $this->drupalCreateNode([
      'type' => $this->contentType,
      'title' => 'Unpublished node',
      'status' => 0,
      'langcode' => 'en',
    ]);
  }

  /**
   * Creates an unpublished node and checks there's no access to it.
   */
  public function testUnpublishedNodeAccess() {
    $this->drupalGet('node/' . $this->node1->id());
    $this->assertSession()->pageTextContains('Access denied');
  }

  /**
   * Creates a published node and checks there's access to it.
   */
  public function testPublishedNodeAccess() {
    $this->node1->set('status', 1);
    $this->node1->set('title', 'Published node');
    $this->node1->save();

    $this->drupalGet('node/' . $this->node1->id());
    $this->assertSession()->pageTextContains('Published node');
  }

  /**
   * @throws \Behat\Mink\Exception\ResponseTextException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function testUnpublishedTranslationAccess() {
    $french_title = 'French title';
    $spanish_title = 'Spanish title';

    $this->node2Fr = $this->drupalCreateNode([
      'type' => $this->contentType,
      'title' => $french_title,
      'status' => 1,
      'langcode' => 'fr',
    ]);
    // Now I should be able to access this node.
    $this->drupalGet('node/' . $this->node2Fr->id());
    $this->assertSession()->pageTextContains($french_title);
    // Add a translation, unpublished.
    $this->node2Es = $this->node2Fr->addTranslation('es', [
      'title' => $spanish_title,
      'status' => 0,
    ]);
    $this->node2Es->save();
    // Now I shouldn't get there.
    $this->drupalGet('/es/node/' . $this->node2Es->id());
    $this->assertSession()->pageTextContains('Access denied');
    // But I should get to the French translation still.
    $this->drupalGet('node/' . $this->node2Fr->id());
    $this->assertSession()->pageTextContains($french_title);
    // Then I try publishing the Spanish version.
    $this->node2Es->set('status', 1);
    $this->drupalGet('/es/node/' . $this->node2Es->id());
    $this->assertSession()->pageTextContains($spanish_title);
    // Unpublishing the French one.
    $this->node2Fr->set('status', 0);
    $this->drupalGet('/fr/node/' . $this->node2Fr->id());
    $this->assertSession()->pageTextContains('Access denied');
  }

}
