<?php

/**
 * @file
 * Test FAQ functionality Base test class. All tests inherits this one.
 * Hugely based on code from the test file block.test by boombatower
 */

/**
 * Base class that is extended by test cases
 */
class FaqTestCase extends DrupalWebTestCase {


  protected $admin_user, $faq_user;
  protected $taxonomy;
  protected $term, $faq1, $faq2;

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('FAQ functionality'),
      'description' => t('Base class. No tests here.'),
      'group' => t('Frequently Asked Questions'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {

    // Install FAQ Module.
    parent::setUp('taxonomy', 'faq');

    // Create and log in user with administer taxonomy permissions.
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer faq', 'administer faq order', 'administer blocks'));
    $this->drupalLogin($this->admin_user);

    $this->faq_user = $this->drupalCreateUser(array('create faq content', 'edit any faq content', 'delete any faq content', 'view faq page', 'access content'));

    // Set up the vocab and terms.
    $this->setupTaxonomy();

    // Categorize questions.
    $this->drupalPost('admin/config/content/faq/categories', array('faq_use_categories' => '1'), t('Save configuration'));

    // Set answer_user as default expert.
    $roles = $this->faq_user->roles;
    end($roles); // Set to last role (the unique one)

    // Start all tests logged out.
    $this->drupalLogout();

  }

  /**
   * Tear the whole thing down again
   */
  function tearDown() {

    // Things to tidy up like vars and stuff
    parent::tearDown();
  }

  /**
   * Generates a random string of ASCII numeric characters (values 48 to 57).
   *
   * @param $length
   *   Length of random string to generate .
   * @return
   *   Randomly generated string.
   */
  protected static function randomNumber($length = 8) {
    $str = '';
    for ($i = 0; $i < $length; $i++) {
      $str .= chr(mt_rand(48, 57));
    }
    return $str;
  }

  /**
   * Verify that current user has no access to page.
   *
   * @param $url
   *   URL to verify.
   */
  function faqVerifyNoAccess($url) {
    // Test that page returns 403 Access Denied
    $this->drupalGet($url);
    $this->assertResponse(403);
  }

  /**
   * Set up the taxonomy - all vocabularies and stuff
   * Values also stored in protected variable $tax
   */
  function setupTaxonomy() {
    // Create vocabulary.
    $this->vocabulary = $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName(8);
    $vocabulary->description = $this->randomName(64);
    $vocabulary->machine_name = drupal_strtolower($this->randomName());
    $vocabulary->help = '';
    $vocabulary->nodes = array('faq' => 'faq');
    $vocabulary->weight = mt_rand(0, 10);
    taxonomy_vocabulary_save($vocabulary);

    $field = array(
      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $this->vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    $this->instance = array(
      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
      'bundle' => 'faq',
      'entity_type' => 'node',
      'widget' => array(
        'type' => 'taxonomy_autocomplete',

      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
        ),
      ),
    );
    field_create_instance($this->instance);

    // Add term
    // Click the last occurrence of the link.
    $this->term = $term = new stdClass();
    $term->name = $this->randomName(8);
    $term->description = $this->randomName(64);
    // Use the first available text format.
    $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);
  }

  /**
   * Test accessing the FAQ page
   */

}

class FaqAccessTestClass extends FaqTestCase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Access to FAQ pages'),
      'description' => t('Access to pages by anonymous user and logged in user with rights.'),
      'group' => t('Frequently Asked Questions'),
    );
  }

  function testFaqAccess() {

    // Verify that anonymous user has no access to the faq page
    $this->faqVerifyNoAccess('faq');

    // Create and login user
    $normal_user = $this->drupalCreateUser();
    $this->drupalLogin($normal_user);
    // Verify that logged in user has no access to the faq page
    $this->faqVerifyNoAccess('faq');
    $this->drupalLogout();

    $view_faq_user = $this->drupalCreateUser(array('view faq page'));
    $this->drupalLogin($view_faq_user);

    // Verify that the faq page is visible and available but empty
    $this->drupalGet('faq');
    $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));

  }
}


class CreateFaqTestCase extends FaqTestCase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Create FAQ node'),
      'description' => t('Add, edit and delete FAQ nodes when FAQ module is enabled.'),
      'group' => t('Frequently Asked Questions'),
    );
  }

  /**
   * Test creating a FAQ node
   */
  function testFaqCreate() {

    // Create and log in user with create FAQ permissions
    $this->admin_user = $this->drupalCreateUser(array('create faq content', 'view faq page'));
    $this->drupalLogin($this->admin_user);

    // Verify that the faq page is visible and available but empty
    $this->drupalGet('faq');
    $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));

    // Fill in the Create FAQ node 1 form and post it
    $langcode = LANGUAGE_NONE;
    $this->faq1 = array();
    $this->faq1['title'] = 'faq1_'. $this->randomName(8);
    $this->faq1[$this->instance['field_name'] . '[' . $langcode .']'] = $this->term->name;
    $this->faq1['detailed_question'] = $this->randomName(16);
    $this->faq1["body[$langcode][0][value]"] = $this->randomName(16);
    $this->drupalPost('node/add/faq', $this->faq1, t('Save'));

    // Check that new FAQ node has actually been created
    $this->assertText(t('FAQ @title has been created.', array('@title' => $this->faq1['title'])));

    // Fill in the Create FAQ node 2 form and post it
    $this->faq2 = array();
    $this->faq2['title'] = 'faq2_'. $this->randomName(8);
    $this->faq2[$this->instance['field_name'] . '[' . $langcode .']'] = $this->randomName(8); // Add new term
    $this->faq2['detailed_question'] = $this->randomName(16);
    $this->faq2["body[$langcode][0][value]"] = $this->randomName(16);
    $this->drupalPost('node/add/faq', $this->faq2, t('Save'));

    // Check that new FAQ node has actually been created
    $this->assertText(t('FAQ @title has been created.', array('@title' => $this->faq2['title'])));


    // New user
    $this->drupalLogout();


    // Verify that logged in user has no access to the faq page
    $this->faqVerifyNoAccess('faq');

    // Check that the FAQ page is available and that the correct term is listed as grouping for the new FAQ node
    $view_faq_user = $this->drupalCreateUser(array('view faq page'));
    $this->drupalLogin($view_faq_user);
    $this->drupalGet('faq');
    $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
    $this->assertText($this->faq1['title'], t('Created FAQ node 1 available on FAQ page.'));
    $this->assertText($this->faq1[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 1 available on FAQ page.'));
    $this->assertText($this->faq2['title'], t('Created FAQ node 2 available on FAQ page.'));
    $this->assertText($this->faq2[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 2 available on FAQ page.'));

    // Navigate to FAQ node created on FAQ page
    $this->clickLink(t($this->faq1['title']));
    $this->assertText(t($this->faq1["body[$langcode][0][value]"]));

    // Enable categorisation of FAQ nodes
    // Create and log in user with create and administer FAQ permissions
    $this->admin_user = $this->drupalCreateUser(array('create faq content', 'view faq page', 'administer faq'));
    $this->drupalLogin($this->admin_user);

    // faq_use_categories
    $faqcfg = array();
    $faqcfg['faq_use_categories'] = '1'; // Enable categorised FAQs
    $this->drupalPost('admin/config/content/faq/categories', $faqcfg, t('Save configuration'));
    $this->drupalLogout();

    $this->drupalLogin($view_faq_user);
    $this->drupalGet('faq');
    $this->assertText(t('Frequently Asked Questions'), t('FAQ page is available for view faq page permissions.'));
    $this->assertText($this->faq1['title'], t('Created FAQ node 1 available on FAQ page.'));
    $this->assertText($this->faq1[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 1 not available on FAQ page.'));
    $this->assertText($this->faq2['title'], t('Created FAQ node 2 available on FAQ page.'));
    $this->assertText($this->faq2[$this->instance['field_name'] . '[' . $langcode .']'], t('Term for node 2 not available on FAQ page.'));

    // TODO: Add update and Delete of FAQ nodes

  }

}


class CRAUDFaqTestCase extends FaqTestCase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('CRUD Question'),
      'description' => t('Create, read, update and delete a question'),
      'group' => t('Frequently Asked Questions'),
    );
  }

  /**
   * Test creating an FAQ and verify its status
   */
  public function testFaqCreate() {

    // Verify that logged in user has no access to the faq page
    $this->faqVerifyNoAccess('faq');

    // Log in user with create FAQ permissions
    $this->drupalLogin($this->faq_user);

    // Create a FAQ node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit[$this->instance['field_name'] . '['. LANGUAGE_NONE .']'] = $this->randomName(8);
    $edit['detailed_question'] = $this->randomName(64);
    $edit['body['. LANGUAGE_NONE .'][0][value]'] = $this->randomString(264);
    $this->drupalPost('node/add/faq', $edit, t('Save'));
    $this->assertText(t('FAQ @title has been created.', array('@title' => $edit['title'])));

    // Check status for FAQ node - should be published
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertTrue($node->status);

    $this->drupalLogout();

    // Verify that anonymous user has no access to the unanswered node display
    //    $this->faqVerifyNoAccess('node/' . $node->nid);
    $this->drupalGet('node/' . $node->nid . '/edit'); // Open edit page with node
    //    $this->assertResponse(200);

    // Create and log in user with view FAQ permissions
    $faq_view_user = $this->drupalCreateUser(array('view faq page'));
    $this->drupalLogin($faq_view_user);

    // Verify visibility on faq page
    $this->drupalGet('faq'); // Load faq page
    $this->assertText($edit['title']); // Node should be listed here
    $this->drupalGet('node/' . $node->nid); // It should also be possible to open the node directly

    // Update FAQ
    // Log in user with answer question. Must also have edit any faq content and view faq page permissions.
    $this->drupalLogin($this->faq_user);
    $edit2['title'] = 'title-' . $this->randomName(8);
    $edit2['body['. LANGUAGE_NONE .'][0][value]']  = 'body-' . $this->randomName(64);
    $this->drupalPost('node/' . $node->nid . '/edit', $edit2, t('Save'));
    $this->assertText(t('FAQ @title has been updated.', array('@title' => $edit2['title'])));
    $this->assertText($edit2['title'], 'Title has changed');
    $this->assertText($edit2['body['. LANGUAGE_NONE .'][0][value]'], 'Body has changed');

    // Delete FAQ
    // Try deleting faq by edit faq permission
    $this->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete'));

    // Log in user with delete FAQ permissions
    $this->drupalLogin($this->admin_user);
    $this->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete'));
    $this->assertText(t('Are you sure you want to delete @title?', array('@title' => $edit2['title'])));
    $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
    $this->assertText(t('FAQ @title has been deleted.', array('@title' => $edit2['title'])));

  }

}
