<?php

abstract class ViewsDataExportBaseTest extends ViewsTestCase {

  protected function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }

    $modules[] = 'views_data_export';
    $modules[] = 'views_ui';
    $modules[] = 'views';
    parent::setUp($modules);

    // Define the schema and views data variable before enabling the test module.
    variable_set('views_test_schema', $this->schemaDefinition());
    variable_set('views_test_views_data', $this->viewsData());
    variable_set('views_test_views_plugins', $this->viewsPlugins());

    module_enable(array('views_test'));
    $this->resetAll();

    // Load the test dataset.
    $data_set = $this->dataSet();
    $query = db_insert('views_test')
      ->fields(array_keys($data_set[0]));
    foreach ($data_set as $record) {
      $query->values($record);
    }
    $query->execute();
    $this->checkPermissions(array(), TRUE);
  }

  /**
   * This function allows to enable views ui from a higher class which can't change the setup function anymore.
   *
   * @TODO
   *   Convert existing setUp functions.
   */
  function enableViewsUi() {
    module_enable(array('views_ui'));
    // @TODO Figure out why it's required to clear the cache here.
    views_module_include('views_default', TRUE);
    views_get_all_views(TRUE);
    menu_rebuild();
  }

  /**
   * The schema definition.
   */
  protected function schemaDefinition() {
    $schema['views_test'] = array(
      'description' => 'Basic test table for Views tests.',
      'fields' => array(
        'id' => array(
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'name' => array(
          'description' => "A person's name",
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'age' => array(
          'description' => "The person's age",
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0),
        'job' => array(
          'description' => "The person's job",
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => 'Undefined',
        ),
        'created' => array(
          'description' => "The creation date of this record",
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'primary key' => array('id'),
      'unique keys' => array(
        'name' => array('name')
      ),
      'indexes' => array(
        'ages' => array('age'),
      ),
    );
    return $schema;
  }

  /**
   * The views data definition.
   */
  protected function viewsData() {
    // Declaration of the base table.
    $data['views_test']['table'] = array(
      'group' => t('Views test'),
      'base' => array(
        'field' => 'id',
        'title' => t('Views test'),
        'help' => t('Users who have created accounts on your site.'),
      ),
    );

    // Declaration of fields.
    $data['views_test']['id'] = array(
      'title' => t('ID'),
      'help' => t('The test data ID'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
    );
    $data['views_test']['name'] = array(
      'title' => t('Name'),
      'help' => t('The name of the person'),
      'field' => array(
        'handler' => 'views_handler_field',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_string',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_string',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
    );
    $data['views_test']['age'] = array(
      'title' => t('Age'),
      'help' => t('The age of the person'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
    );
    $data['views_test']['job'] = array(
      'title' => t('Job'),
      'help' => t('The job of the person'),
      'field' => array(
        'handler' => 'views_handler_field',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_string',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_string',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
    );
    $data['views_test']['created'] = array(
      'title' => t('Created'),
      'help' => t('The creation date of this record'),
      'field' => array(
        'handler' => 'views_handler_field_date',
        'click sortable' => TRUE,
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_date',
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_date',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort_date',
      ),
    );
    return $data;
  }

  protected function viewsPlugins() {
    return array();
  }

  /**
   * A very simple test dataset.
   */
  protected function dataSet() {
    return array(
      array(
        'name' => 'John',
        'age' => 25,
        'job' => 'Singer',
        'created' => gmmktime(0, 0, 0, 1, 1, 2000),
      ),
      array(
        'name' => 'George',
        'age' => 27,
        'job' => 'Singer',
        'created' => gmmktime(0, 0, 0, 1, 2, 2000),
      ),
      array(
        'name' => 'Ringo',
        'age' => 28,
        'job' => 'Drummer',
        'created' => gmmktime(6, 30, 30, 1, 1, 2000),
      ),
      array(
        'name' => 'Paul',
        'age' => 26,
        'job' => 'Songwriter',
        'created' => gmmktime(6, 0, 0, 1, 1, 2000),
      ),
      array(
        'name' => 'Meredith',
        'age' => 30,
        'job' => 'Speaker',
        'created' => gmmktime(6, 30, 10, 1, 1, 2000),
      ),
    );
  }

  /**
   * Build and return a basic view of the views_test table.
   *
   * @return view
   */
  protected function getBasicView() {
    views_include('view');

    // Create the basic view.
    $view = new view();
    $view->vid = 'test_view';
    $view->add_display('default');
    $view->base_table = 'views_test';

    // Set up the fields we need.
    $display = $view->new_display('default', 'Master', 'default');
    $display->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Set up the sort order.
    $display->override_option('sorts', array(
      'id' => array(
        'order' => 'ASC',
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
    ));

    // Set up the pager.
    $display->override_option('pager', array(
      'type' => 'none',
      'options' => array('offset' => 0),
    ));

    return $view;
  }

  public function logViewResult($result, $prefix = 'View result:<br>') {
    $this->verbose($prefix . '<br><pre>' . check_plain($result) .'</pre>');
  }

  public function assertBatchedExportEqual($path, $expected, $message) {
    $this->drupalGet($path);
    $output = $this->drupalGetContent();
    $this->logViewResult($output);
    $this->logViewResult($expected, 'Expected result:<br>');
    $this->assertEqual($this->normaliseString($output), $this->normaliseString($expected), $message);
  }

  public function assertExportEqual($a, $b, $message) {
    $this->logViewResult($a);
    $this->logViewResult($b, 'Expected result:<br>');
    $this->assertEqual($this->normaliseString($a), $this->normaliseString($b), $message);
  }

  protected function normaliseString($s) {
    // Normalize line endings
    // Convert all line-endings to UNIX format
    $s = str_replace("\r\n", "\n", $s);
    $s = str_replace("\r", "\n", $s);
    $s = trim($s);
    return $s;
  }

}

abstract class ViewsDataExportSimpleExportTest extends ViewsDataExportBaseTest {

  protected $vde_export_type;

  /**
   * Tests the non-batched export functionality for this style.
   */
  public function testNonBatchedExport() {
    $path = 'vde_test/' . $this->randomName();
    list($view, $expected) = $this->getExportView($path);
    // Save this view so we can hit the path.
    $view->save();
    // Ensure that the menu router system is rebuilt on the next page load.
    variable_set('menu_rebuild_needed', TRUE);

    $this->drupalGet($path);
    $result = $this->drupalGetContent();

    $this->assertExportEqual($result, $expected, 'Non batched ' . $this->vde_export_type . ' export matched expected output.');
  }

  /**
   * Tests the batched export functionality for this style.
   */
  public function testBatchedExport() {
    $path = 'vde_test/' . $this->randomName();
    list($view, $expected) = $this->getExportView($path);
    $display = &$view->display['vde_test']->handler;
    // Set this view to be batched.
    $display->override_option('use_batch', 'batch');
    // Save this view so we can hit the path.
    $view->save();
    // Ensure that the menu router system is rebuilt on the next page load.
    variable_set('menu_rebuild_needed', TRUE);

    $this->assertBatchedExportEqual($path, $expected, 'Batched ' . $this->vde_export_type . ' export matched expected output.');
  }

  /**
   * Get a very basic view and expected output for this style.
   *
   * @return
   *   An array containing two elements:
   *   - A View object, for the export.
   *   - The expected out from that view, if is was executed without further
   *     changes.
   */
  abstract protected function getExportView($path = 'vde_test');

  /**
   * Build and return a basic view of the views_test table.
   *
   * @return view
   */
  protected function getBasicExportView() {
    views_include('view');

    // Create the basic view.
    $view = new view();
    $view->vid = 'new';
    $view->base_table = 'views_test';

    // Set up the fields we need.
    $display = $view->new_display('default', 'Master', 'default');

    $display->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Set up the sort order.
    $display->override_option('sorts', array(
      'id' => array(
        'order' => 'ASC',
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
    ));

    // Set up the pager.
    $display->override_option('pager', array(
      'type' => 'none',
      'options' => array('offset' => 0),
    ));

    return $view;
  }

  /**
   * Get a view that's our basic view, but with hide if empty/0 support.
   *
   * We add this to the 'age' field.
   */
  protected function getHideIfEmptyExportView() {
    $view = $this->getBasicExportView();

    $display = $view->display['default']->handler;

    $display->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
        // Hide their name if its empty.
        'hide_empty' => TRUE,
        // But we don't hide it if it's: 0.
        'empty_zero' => FALSE,
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
        // Hide their age if it's empty.
        'hide_empty' => TRUE,
        'empty_zero' => TRUE,
      ),
    ));

    return $view;
  }

  /**
   * Execute a given view very simply.
   *
   * This will take a view, and add a display plugin of the correct export type,
   * and then run it and compare it with the expected output.
   */
  protected function executeAndCompareGivenView(view $view, $expected, $message = '', $style_options = array()) {
    $path = 'vde_test/' . $this->randomName();

    $display = $view->new_display('views_data_export', 'Data export', 'vde_test');
    $display->override_option('style_plugin', $this->getStylePluginName());
    $display->override_option('style_options', $style_options);
    $display->override_option('path', $path);

    // Save this view so we can hit the path.
    $view->save();

    // Ensure that the menu router system is rebuilt on the next page load.
    variable_set('menu_rebuild_needed', TRUE);

    $this->drupalGet($path);
    $result = $this->drupalGetContent();

    $this->assertExportEqual($result, $expected, $message);
  }

  /**
   * Return the name of the style plugin represented by this test.
   */
  abstract protected function getStylePluginName();
}
