<?php

class QuicktabsAdminTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Quicktabs tests'),
      'description' => t('Add, edit and delete quicktabs.'),
      'group' => t('Quicktabs'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('ctools','quicktabs');

    // Create and login user
    $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer quicktabs', 'administer nodes'));
    $this->drupalLogin($admin_user);
    
    // Create some nodes that we can populate our tabs with.
    for ($i = 0; $i < 5; $i++) {
      $node = new stdClass();
      $node->type = 'page';
      $node->title = 'This is node number '. ($i+1);
      $node->body[LANGUAGE_NONE][0]['value'] = $this->randomString(255);
      node_object_prepare($node);
      node_save($node);
    }
  }

  /**
   * Create a Quicktabs instance through the UI and ensure that it is saved properly.
   */
  function testQuicktabsAdmin() {
    // Add a new Quicktabs instance using the UI.
    $edit = array(
      'machine_name' => strtolower($this->randomName()),
      'title' => $this->randomName(),
      'ajax' => 0,
      'hide_empty_tabs' => FALSE,
      'renderer' => 'quicktabs',
    );
    
    $saved = $edit;
    // We'll be using the $saved array to compare against the Quicktabs instance
    // that gets created. However, hierarchical form elements are dealt with
    // differenly so we can't include them in the $saved array like this.
    $tab_title_first = $this->randomName();
    $tab_title_second = $this->randomName();
    $edit += array(
      'tabs[0][type]' => 'node',
      'tabs[0][node][nid]' => 1,
      'tabs[0][title]' => $tab_title_first,
      'tabs[0][weight]' => 0,
      'tabs[1][type]' => 'node',
      'tabs[1][node][nid]' => 2,
      'tabs[1][title]' => $tab_title_second,
      'tabs[1][weight]' => 1,
    );
    // Now add on the tabs info to the $saved array - it's the same as what we
    // put in the edit form but we need it in proper array format.
    $saved['tabs'] = array(0 => array('type' => 'node', 'nid' => 1, 'title' => $tab_title_first, 'weight' => 0), 1 => array('type' => 'node', 'nid' => 2, 'title' => $tab_title_second, 'weight' => 1));
    $this->drupalPost('admin/structure/quicktabs/add', $edit, t('Save'));

    // Check that the quicktabs object is in the database.
    $quicktabs = quicktabs_load($edit['machine_name']);
    $this->assertTrue($quicktabs != FALSE, t('Quicktabs instance found in database'));
  
    // Check each individual property of the quicktabs and make sure it was set.
    foreach ($saved as $property => $value) {
      if ($property == 'tabs') {
        // Add some extra default values that we didn't include on the form, for
        // the sake of comparing the two tabs arrays.
        foreach ($value as &$val) {
          $val += array('teaser' => 0, 'hide_title' => 1);
        }
      }
      $this->assertEqual($quicktabs->$property, $value, t('Quicktabs property %property properly saved.', array('%property' => $property)));
    }
    
    // Edit the Quicktabs instance through the UI.
    $edit = array(
      'title' => $this->randomName(),
      'ajax' => 1,
      'hide_empty_tabs' => TRUE,
      'renderer' => 'ui_tabs',
      'default_tab' => 1,
    );
    
    $saved = $edit;
    $tab_title_first = $this->randomName();
    $tab_title_second = $this->randomName();
    $edit += array(
      'tabs[0][title]' => $tab_title_first,
      'tabs[0][weight]' => 1,
      'tabs[0][node][nid]' => 3,
      'tabs[0][node][teaser]' => 1,
      'tabs[0][node][hide_title]' => FALSE,
      'tabs[1][title]' => $tab_title_second,
      'tabs[1][weight]' => 0,
      'tabs[1][node][nid]' => 4,
      'tabs[1][node][teaser]' => FALSE,
      'tabs[1][node][hide_title]' => 1,
    );
    $saved['tabs'] = array(0 => array('type' => 'node', 'nid' => 4, 'title' => $tab_title_second, 'weight' => 0, 'teaser' => 0, 'hide_title' => 1), 1 => array('type' => 'node', 'nid' => 3, 'title' => $tab_title_first, 'weight' => 1, 'teaser' => 1, 'hide_title' => 0));
    $this->drupalPost('admin/structure/quicktabs/manage/'. $quicktabs->machine_name .'/edit', $edit, t('Save'));
    
    // Reset static vars because ctools will have cached the original $quicktabs object
    drupal_static_reset();
    // Check that the quicktabs object is in the database.
    $edited_qt = quicktabs_load($quicktabs->machine_name);
    $this->assertTrue($edited_qt != FALSE, t('Quicktabs instance found in database'));
  
    // Check each individual property of the quicktabs and make sure it was set.
    foreach ($saved as $property => $value) {
      $this->assertEqual($edited_qt->$property, $value, t('Quicktabs property %property properly saved.', array('%property' => $property)));
    }

    // Delete the Quicktabs instance through the UI.
    $this->drupalPost('admin/structure/quicktabs/manage/'. $quicktabs->machine_name .'/delete', array(), t('Delete'));
    // Reset static vars because ctools will have cached the original $quicktabs object
    drupal_static_reset();
    // Check that the quicktabs object is no longer in the database.
    $this->assertNull(quicktabs_load($quicktabs->machine_name), t('Quicktabs instance not found in database'));
  }

}

