class ViewsSqlTest

Same name in other branches
  1. 7.x-3.x tests/views_query.test \ViewsSqlTest

Hierarchy

Expanded class hierarchy of ViewsSqlTest

File

tests/views_query.test, line 92

View source
abstract class ViewsSqlTest extends ViewsTestCase {
    protected function setUp() {
        parent::setUp('views', 'views_ui');
        // 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',
        ));
        drupal_install_modules(array(
            'views_test',
        ));
        drupal_get_schema('views_test', TRUE);
        // Load the test dataset.
        foreach ($this->dataSet() as $record) {
            drupal_write_record('views_test', $record);
        }
        $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.inc', 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;
    }
    protected function viewsPlugins() {
        return array();
    }
    
    /**
     * 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;
    }
    
    /**
     * 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.
     */
    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', 'Defaults', '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',
            ),
        ));
        return $view;
    }
    
    /**
     * Helper function to execute a view with debugging.
     */
    protected function executeView($view) {
        $view->execute();
        $this->verbose('<pre>Executed view: ' . print_r($view->build_info, TRUE) . '</pre>');
    }

}

Members

Title Sort descending Modifiers Object type Summary
ViewsSqlTest::dataSet protected function A very simple test dataset.
ViewsSqlTest::enableViewsUi function This function allows to enable views ui from a higher class which can't change the setup function anymore.
ViewsSqlTest::executeView protected function Helper function to execute a view with debugging.
ViewsSqlTest::getBasicView protected function Build and return a basic view of the views_test table.
ViewsSqlTest::schemaDefinition protected function The schema definition.
ViewsSqlTest::setUp protected function
ViewsSqlTest::viewsData protected function The views data definition.
ViewsSqlTest::viewsPlugins protected function
ViewsTestCase::$sort_column protected property
ViewsTestCase::$sort_order protected property
ViewsTestCase::assertIdenticalResultset protected function Helper function: verify a result set returned by view.
ViewsTestCase::helperButtonHasLabel protected function Helper function to check whether a button with a certain id exists and has a certain label.
ViewsTestCase::helperCompareFunction protected function Helper comparison function for orderResultSet().
ViewsTestCase::orderResultSet protected function Helper function: order an array of array based on a column.