<?php

/**
 * @file
 * Views date pager test.
 */

/**
 * Test Date views pager functions.
 */
class ViewsPagerTestCase extends DrupalWebTestCase {

  /**
   * Test info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Date views pager skipping test',
      'description' => "Views date pager, option to skip empty pages test",
      'group' => 'date',
      'dependencies' => array('ctools', 'views'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    $modules[] = 'date_views';
    $modules[] = 'ctools';
    $modules[] = 'views';
    $modules[] = 'views_ui';
    parent::setUp($modules);

    // Create admin user and login.
    $perms = array('administer views', 'administer site configuration');
    $admin_user = $this->drupalCreateUser($perms);
    $this->drupalLogin($admin_user);

    // Create a new view for test.
    $view = new view();
    $view->name = 'test_date_pager';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_date_pager';
    $view->core = 7;
    $view->api_version = '3.0';
    $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['title'] = 'test_date_pager';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'date_views_pager';
    $handler->display->display_options['pager']['options']['skip_empty_pages'] = 1;
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'node';
    /* 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']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Post date */
    $handler->display->display_options['sorts']['created']['id'] = 'created';
    $handler->display->display_options['sorts']['created']['table'] = 'node';
    $handler->display->display_options['sorts']['created']['field'] = 'created';
    $handler->display->display_options['sorts']['created']['order'] = 'DESC';
    /* Contextual filter: Date: Date (node) */
    $handler->display->display_options['arguments']['date_argument']['id'] = 'date_argument';
    $handler->display->display_options['arguments']['date_argument']['table'] = 'node';
    $handler->display->display_options['arguments']['date_argument']['field'] = 'date_argument';
    $handler->display->display_options['arguments']['date_argument']['default_action'] = 'default';
    $handler->display->display_options['arguments']['date_argument']['default_argument_type'] = 'date';
    $handler->display->display_options['arguments']['date_argument']['summary']['format'] = 'default_summary';
    $handler->display->display_options['arguments']['date_argument']['granularity'] = 'hour';
    $handler->display->display_options['arguments']['date_argument']['date_fields'] = array(
      'node.created' => 'node.created',
    );
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

    /* Display: Page */
    $handler = $view->new_display('page', 'Page', 'page_1');
    $handler->display->display_options['path'] = 'test_date_pager';

    $view->save();
  }

  /**
   * Test pager skipping.
   */
  public function testPagerSkipping() {
    // Go to view admin page.
    $this->drupalGet('admin/structure/views/view/display/test_date_pager/edit');
    // Go to pager options.
    $this->drupalGet('admin/structure/views/nojs/display/test_date_pager/default/pager_options');
    // Check if "Skip empty pages" text - exist.
    $this->assertText('Skip empty pages');
    // Check if field and it's value is correct.
    $this->assertFieldByName('pager_options[skip_empty_pages]', '1');
    // Go back to view admin page.
    $this->drupalGet('admin/structure/views/view/display/test_date_pager/edit');
    // Check if pager on empty page are gone.
    $this->assertNoText('« Prev', 'Previous pager does not exist');
    $this->assertNoText('Next »', 'Next pager does not exist');
  }

  /**
   * Test the view page has no PHP warnings.
   */
  public function testPagerWarning() {
    $this->drupalCreateNode(array('type' => 'blog'));

    // Set pager to skip empty pages.
    $edit = array(
      'pager_options[skip_empty_pages]' => FALSE,
    );
    $this->drupalPost('admin/structure/views/nojs/display/test_date_pager/default/pager_options', $edit, t('Apply'));

    // Save the view.
    $this->drupalPost('admin/structure/views/view/test_date_pager/edit', array(), t('Save'));

    // Visit view page. This will throw error, if any PHP warnings or errors.
    $this->drupalGet('test_date_pager');
  }

}
