<?php

/**
 * @file
 * Test protected node password page display options.
 */

/**
 * Configure protected_node to use per node password.
 */
class ProtectedNodePageDisplay extends ProtectedNodeBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Protected node password page display options',
      'description' => "This tests what is displayed on the enter password page",
      'group' => 'Protected Node',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Log in an Admin.
    $this->drupalLogin($this->adminUser);
    // Submit the configuration form.
    $protected_node_settings = array(
      'protected_node_use_global_password' => PROTECTED_NODE_PER_NODE_PASSWORD,
    );
    $this->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));
  }

  /**
   * Test function.
   *
   * Test that default display text is displayed (not showing title).
   */
  public function testDefaultHideTitle() {
    // Create a protected node without showing its title.
    $node = $this->createProtectedNode(TRUE, FALSE);

    // User sees the default text.
    $this->drupalLogin($this->normalAccessAllowedUser);
    $this->drupalGet('node/' . $node->nid);

    $text = t('The @node_type @node_title you are trying to view is password protected. Please enter the password below to proceed.',
      array('@node_type' => node_type_get_name($node), '@node_title' => ''));
    $this->assertText($text, "Default text displayed on the enter password page does not show the node title", $this->group);
  }

  /**
   * Test function.
   *
   * Test that default display text is displayed (showing title).
   */
  public function testDefaultShowTitle() {
    // Create a protected node that show its title.
    $node = $this->createProtectedNode(TRUE, TRUE);

    // User sees the default text including node title.
    $this->drupalLogin($this->normalAccessAllowedUser);
    $this->drupalGet('node/' . $node->nid);

    $text = t('The @node_type @node_title you are trying to view is password protected. Please enter the password below to proceed.',
      array('@node_type' => node_type_get_name($node), '@node_title' => '"' . $node->title . '"'));

    $this->assertText($text, "Default text displayed on the enter password page contains the node title if asked", $this->group);
  }

  /**
   * Test function.
   *
   * Test that the password page description site config setting works.
   */
  public function testGlobalDescriptionOnly() {

    $global_description = $this->randomName(32);

    $this->drupalLogin($this->adminUser);
    $protected_node_settings = array(
      'protected_node_use_global_password' => PROTECTED_NODE_PER_NODE_PASSWORD,
      'protected_node_description' => $global_description,
    );
    $this->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));

    // $show_title_option_available is false since there is a custom description
    // set.
    $node = $this->createProtectedNode(FALSE);

    // User sees the set global text.
    $this->drupalLogin($this->normalAccessAllowedUser);
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($global_description, "Custom global text is displayed on the enter password page", $this->group);
  }

  /**
   * Test function.
   *
   * Test that the password page description content type setting is displayed.
   */
  public function testContentTypeDescriptionOnly() {

    $content_type_description = $this->randomName(16);

    // Set global text.
    $this->drupalLogin($this->adminUser);
    $settings = array(
      'protected_node_description' => $content_type_description,
    );
    $this->drupalPost('admin/structure/types/manage/page', $settings, t('Save content type'));

    // $show_title_option_available is FALSE since there is a custom description
    // set.
    $node = $this->createProtectedNode(FALSE);

    // User sees the set global text.
    $this->drupalLogin($this->normalAccessAllowedUser);
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($content_type_description, "Custom per content type text displayed on the enter password page", $this->group);
  }

  /**
   * Test function.
   *
   * Test that the password page description per content type is displayed
   * instead instead of the global description.
   */
  public function testContentTypeWithGlobalDescriptionSet() {

    $global_description = $this->randomName(32);
    $content_type_description = $this->randomName(16);

    // Set global text.
    $this->drupalLogin($this->adminUser);
    $protected_node_settings = array(
      'protected_node_use_global_password' => PROTECTED_NODE_PER_NODE_PASSWORD,
      'protected_node_description' => $global_description,
    );
    $this->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));

    // Set content type text.
    $settings = array(
      'protected_node_description' => $content_type_description,
    );
    $this->drupalPost('admin/structure/types/manage/page', $settings, t('Save content type'));

    // $show_title_option_available is FALSE since there is a custom description
    // set.
    $node = $this->createProtectedNode(FALSE);

    // User sees the set content type text.
    $this->drupalLogin($this->normalAccessAllowedUser);
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($content_type_description, "Custom content type description displayed instead of the global description on the enter password page", $this->group);
  }

  /**
   * Helper method to create a protected node.
   *
   * @param bool $show_title_option_available
   *   TRUE if the show title option can be set in the UI, FALSE if it is
   *   hidden.
   * @param bool $show_title
   *   Whether to show the node title or not. Has no effect if
   *   $show_title_option_available is FALSE.
   *
   * @return object
   *   A node object.
   */
  public function createProtectedNode($show_title_option_available, $show_title = FALSE) {
    // Log in as Admin.
    $this->drupalLogin($this->adminUser);
    // Generate random password.
    $password = $this->randomName(10);

    $node_data = array(
      'title' => $this->randomName(8),
      'body[und][0][value]' => $this->randomName(32),
      'protected_node_is_protected' => TRUE,
      'protected_node_passwd[pass1]' => $password,
      'protected_node_passwd[pass2]' => $password,
    );
    if ($show_title_option_available) {
      $node_data += array(
        'protected_node_show_title' => $show_title,
      );
    }
    $this->drupalPost('node/add/page', $node_data, t('Save'));

    // Once the node created logout the user.
    $this->drupalLogout();

    return $this->drupalGetNodeByTitle($node_data['title']);
  }

}
