<?php

/**
 * @file
 * Definition of Drupal\views\Tests\ArgumentDefaultTest.
 */

namespace Drupal\views\Tests;

use Drupal\views\View;

/**
 * Basic test for pluggable argument default.
 */
class ArgumentDefaultTest extends ViewsSqlTest {

  protected $profile = 'standard';

  public static function getInfo() {
    return array(
      'name' => 'Argument default',
      'description' => 'Tests pluggable argument_default for views.',
      'group' => 'Views Plugins'
    );
  }

  public function setUp() {
    parent::setUp();

    $this->random = $this->randomString();
  }

  /**
   * Tests the use of a default argument plugin that provides no options.
   */
  function testArgumentDefaultNoOptions() {
    module_enable(array('views_test'));
    $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
    $this->drupalLogin($admin_user);

    // The current_user plugin has no options form, and should pass validation.
    $argument_type = 'current_user';
    $edit = array(
      'options[default_argument_type]' => $argument_type,
    );
    $this->drupalPost('admin/structure/views/nojs/config-item/test_argument_default_current_user/default/argument/uid', $edit, t('Apply'));

    // Note, the undefined index error has two spaces after it.
    $error = array(
      '%type' => 'Notice',
      '!message' => 'Undefined index:  ' . $argument_type,
      '%function' => 'views_handler_argument->options_validate()',
    );
    $message = t('%type: !message in %function', $error);
    $this->assertNoRaw($message, t('Did not find error message: !message.', array('!message' => $message)));
  }

  /**
   * Tests fixed default argument.
   */
  function testArgumentDefaultFixed() {
    $view = $this->view_argument_default_fixed();

    $view->set_display('default');
    $view->pre_execute();
    $view->init_handlers();

    $this->assertEqual($view->argument['null']->get_default_argument(), $this->random, 'Fixed argument should be used by default.');

    $view->destroy();

    // Make sure that a normal argument provided is used
    $view = $this->view_argument_default_fixed();

    $view->set_display('default');
    $random_string = $this->randomString();
    $view->execute_display('default', array($random_string));

    $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.');
  }

  /**
   * @todo Test php default argument.
   */
  //function testArgumentDefaultPhp() {}

  /**
   * @todo Test node default argument.
   */
  //function testArgumentDefaultNode() {}

  function view_argument_default_fixed() {
    $view = new View();
    $view->name = 'test_argument_default_fixed';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['access']['type'] = 'none';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['pager']['options']['offset'] = '0';
    $handler->display->display_options['pager']['options']['id'] = '0';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
    $handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
    $handler->display->display_options['fields']['title']['alter']['trim'] = 0;
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
    $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
    $handler->display->display_options['fields']['title']['alter']['html'] = 0;
    $handler->display->display_options['fields']['title']['hide_empty'] = 0;
    $handler->display->display_options['fields']['title']['empty_zero'] = 0;
    $handler->display->display_options['fields']['title']['link_to_node'] = 0;
    /* Argument: Global: Null */
    $handler->display->display_options['arguments']['null']['id'] = 'null';
    $handler->display->display_options['arguments']['null']['table'] = 'views';
    $handler->display->display_options['arguments']['null']['field'] = 'null';
    $handler->display->display_options['arguments']['null']['default_action'] = 'default';
    $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary';
    $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed';
    $handler->display->display_options['arguments']['null']['default_argument_options']['argument'] = $this->random;
    $handler->display->display_options['arguments']['null']['must_not_be'] = 0;

    return $view;
  }
}
