<?php

/**
 * @file
 * Test protected node email functionality.
 */

/**
 * Configure protected_node to send email.
 */
class ProtectedNodeMail extends ProtectedNodeBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Protected node email feature',
      'description' => "This tests mail features",
      '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,
      'protected_node_email_activation' => 1,
      'protected_node_random_password' => 1,
    );
    $this->drupalPost('admin/config/content/protected_node', $protected_node_settings, t('Save configuration'));
  }

  /**
   * Test that a mail is send when a protected node is created.
   */
  public function testSendMail() {
    // Log in as Admin.
    $this->drupalLogin($this->adminUser);
    // Generate random password.
    $password = $this->randomName(10);
    // To parameter.
    $to = 'example@example.com';
    // Create a new page node.
    $this->createProtectedNode($password, $to);
    // Once the node created logout the User.
    $this->drupalLogout();

    $mails = $this->drupalGetMails();

    // Check mail subject.
    $this->assertMail('subject', protected_node_email_subject(), 'The last message subject was "My latest email"');

    // Check mail to parameter.
    $this->assertEqual($to, $mails[0]['to'], "The mail's to parameter is equal to " . $to, $this->group);
  }

  /**
   * Test that a random password is created and saved when sending an email.
   */
  public function testRandomPassword() {
    // Log in as Admin.
    $this->drupalLogin($this->adminUser);
    // Don't set a password.
    $password = '';
    // To parameter.
    $to = 'example@example.com';
    // Create a new page node.
    $node = $this->createProtectedNode($password, $to);
    // Once the node created logout the User.
    $this->drupalLogout();

    // Check that a password has been generated.
    $generated_password = $node->protected_node_passwd;
    $this->assertTrue(!empty($generated_password), "Generated password exists.");

    $mails = $this->drupalGetMails();

    // Check that the default mail body contains the password.
    $generated_password_in_mail = FALSE;
    $clear_password = $mails[0]['params']['node']->protected_node_clear_passwd;
    if (strpos($mails[0]['body'], $clear_password)) {
      $generated_password_in_mail = TRUE;
    }
    $this->assertTrue($generated_password_in_mail, "The mail's body contains the randomly generated password.", $this->group);
  }

  /**
   * Helper method to create a protected node.
   *
   * Please make sure the user has the permission to create the node before
   * calling the method.
   *
   * @param string $password
   *   A password.
   * @param string $to
   *   An email recipient.
   *
   * @return object
   *   A node object.
   */
  public function createProtectedNode($password, $to) {
    // Add a new page node that is protected.
    $node_title = $this->randomName(8);
    $node_data = array(
      'title' => $node_title,
      'body[und][0][value]' => $this->randomName(32),
      'protected_node_is_protected' => TRUE,
      'protected_node_passwd[pass1]' => $password,
      'protected_node_passwd[pass2]' => $password,
      'protected_node_emails' => $to,
    );
    $this->drupalPost('node/add/page', $node_data, t('Save'));

    return $this->drupalGetNodeByTitle($node_title);
  }

}
