<?php

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

namespace Drupal\views\Tests;

/**
 * Tests basic functions from the Views module.
 */
class ModuleTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => 'Tests views.module',
      'description' => 'Tests some basic functions of views.module',
      'group' => 'Views',
    );
  }

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

  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_previous'] = array();
    $data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id');
    $data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id');
    $data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age');
    $data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age');
    $data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name');
    $data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name');

    return $data;
  }

  public function test_views_trim_text() {
    // Test unicode, @see http://drupal.org/node/513396#comment-2839416
    $text = array(
      'Tuy nhiên, những hi vọng',
      'Giả sử chúng tôi có 3 Apple',
      'siêu nhỏ này là bộ xử lý',
      'Di động của nhà sản xuất Phần Lan',
      'khoảng cách từ đại lí đến',
      'của hãng bao gồm ba dòng',
      'сд асд асд ас',
      'асд асд асд ас'
    );
    // Just test maxlength without word boundry.
    $alter = array(
      'max_length' => 10,
    );
    $expect = array(
      'Tuy nhiên,',
      'Giả sử chú',
      'siêu nhỏ n',
      'Di động củ',
      'khoảng các',
      'của hãng b',
      'сд асд асд',
      'асд асд ас',
    );

    foreach ($text as $key => $line) {
      $result_text = views_trim_text($alter, $line);
      $this->assertEqual($result_text, $expect[$key]);
    }

    // Test also word_boundary
    $alter['word_boundary'] = TRUE;
    $expect = array(
      'Tuy nhiên',
      'Giả sử',
      'siêu nhỏ',
      'Di động',
      'khoảng',
      'của hãng',
      'сд асд',
      'асд асд',
    );

    foreach ($text as $key => $line) {
      $result_text = views_trim_text($alter, $line);
      $this->assertEqual($result_text, $expect[$key]);
    }
  }

  /**
   * Tests the dynamic includes of templates via module feature.
   */
  function testModuleTemplates() {
    $views_status = variable_get('views_defaults', array());
    $views_status['frontpage'] = FALSE; // false is enabled
    variable_set('views_defaults', $views_status);

    $existing = array();
    $type = array();
    $theme = array();
    $path = array();
    $registry = views_theme($existing, $type, $theme, $path);
    $this->assertTrue(isset($registry['views_view__frontpage']));
  }

  /**
   * Tests the views_get_handler method.
   */
  function testviews_get_handler() {
    $types = array('field', 'area', 'filter');
    foreach ($types as $type) {
      $handler = views_get_handler($this->randomName(), $this->randomName(), $type);
      $this->assertEqual('Drupal\views\Plugin\views\\' . $type . '\Broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
    }

    $views_data = $this->viewsData();
    $test_tables = array('views_test' => array('id', 'name'));
    foreach ($test_tables as $table => $fields) {
      foreach ($fields as $field) {
        $data = $views_data[$table][$field];
        foreach ($data as $id => $field_data) {
          if (!in_array($id, array('title', 'help'))) {
            $handler = views_get_handler($table, $field, $id);
            $this->assertInstanceHandler($handler, $table, $field, $id);
          }
        }
      }
    }

    // Test the automatic conversion feature.

    // Test the automatic table renaming.
    $handler = views_get_handler('views_test_previous', 'id', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
    $handler = views_get_handler('views_test_previous', 'id', 'filter');
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');

    // Test the automatic field renaming.
    $handler = views_get_handler('views_test', 'age_previous', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'age', 'field');
    $handler = views_get_handler('views_test', 'age_previous', 'sort');
    $this->assertInstanceHandler($handler, 'views_test', 'age', 'sort');

    // Test the automatic table and field renaming.
    $handler = views_get_handler('views_test_previous', 'name_previous', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'name', 'field');
    $handler = views_get_handler('views_test_previous', 'name_previous', 'argument');
    $this->assertInstanceHandler($handler, 'views_test', 'name', 'argument');

    // Test the override handler feature.
    $handler = views_get_handler('views_test', 'job', 'filter', 'string');
    $this->assertEqual('Drupal\\views\\Plugin\\views\\filter\\String', get_class($handler));
  }

  /**
   * Ensure that a certain handler is a instance of a certain table/field.
   */
  function assertInstanceHandler($handler, $table, $field, $id) {
    $table_data = views_fetch_data($table);
    $field_data = $table_data[$field][$id];

    $this->assertEqual($field_data['id'], $handler->getPluginId());
  }
}
