view sites/all/modules/sparql_views/tests/sparql_views_basic.test @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
line wrap: on
line source
<?php

/**
 * Tests basic query functionality.
 *
 * This ensures that the query plugin itself can be used and is able to run
 * queries. It does not test whether SPARQL Views resource fields are available
 * and can be used in queries.
 */
class SparqlViewsBasicQueryTest extends SparqlViewsQueryTest {
  
  public static function getInfo() {
    return array(
      'name' => 'Tests basic SPARQL Views querying',
      'description' => 'Tests some basic functions of SPARQL Views',
      'group' => 'SPARQL Views',
    );
  }

  protected function setUp() {
    parent::setUp();
  }

  public function testFieldQuery() {
    $view = $this->getBasicView();

    // Execute the view.
    $view->execute();

    // Test that the query pulled something from the endpoint.
    $this->assertEqual(10, count($view->result), t('The number of returned rows match.'));

    // Test that the query pulled foaf:name from the endpoint.
    $this->assertTrue($view->result[0]->{$this->field_name}, t('The query returned values for foaf:name.'));
  }

  public function testFilterRdfTypeQuery() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    $expected_clause = "/{$this->main_resource} rdf:type foaf:Person\;/";

    $rdftype_filter = array(
      'rdftype' => array(
        'operator' => '=',
        'value' => 'foaf:Person',
        'group' => '0',
        'exposed' => FALSE,
        'expose' => array(
          'operator' => FALSE,
          'label' => '',
        ),
        'id' => $this->main_resource_rdftype,
        'table' => $endpoint->table_id,
        'field' => $this->main_resource_rdftype,
        'relationship' => 'none',
      ),
    );

    $view->display['default']->handler->override_option('filters', $rdftype_filter);

    // Execute the view.
    $view->execute();

    $query = $view->build_info['query'];

    // Test that the query is built properly.
    $this->assertTrue((preg_match($expected_clause, $query) == TRUE), t('RDF type filter adds type restriction.'));
  }

  public function testFilterLanguageQuery() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    $existing_fields = $view->display['default']->handler->get_option('fields');
    $new_fields = array(
      'field_affiliation' => array(
        'id' => $this->field_affiliation,
        'table' => $endpoint->table_id,
        'field' => $this->field_affiliation,
        'relationship' => 'none',
      ),
    );
    $view->display['default']->handler->override_option('fields', array_merge($existing_fields, $new_fields));

    $lang_filter = array(
      'language' => array(
        'operator' => '=',
        'value' => 'ie',
        'group' => '0',
        'exposed' => FALSE,
        'expose' => array(
          'operator' => FALSE,
          'label' => '',
        ),
        'id' => $this->field_affiliation . '_language',
        'table' => $endpoint->table_id,
        'field' => $this->field_affiliation . '_language',
        'relationship' => 'none',
      ),
    );

    $view->display['default']->handler->override_option('filters', $lang_filter);

    // Execute the view.
    $view->execute();

    $query = $view->build_info['query'];

    // Test that the query is built properly.
    $this->assertTrue(count($view->result) == 1 && preg_match('/Gaillimh/', $view->result[0]->{$this->field_affiliation}), t('Filter (language) query returns correct result.'));  }

  public function testRelationshipQuery() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    $view->display['default']->handler->override_option('relationships', array(
      $this->rel_1 => array(
        'id' => $this->rel_1,
        'table' => $endpoint->table_id,
        'field' => $this->rel_1,
        'relationship' => 'none',
      ),
    ));

    $existing_fields = $view->display['default']->handler->get_option('fields');
    $new_fields = array(
      $this->rel_1_field_name => array(
        'id' => $this->rel_1_field_name,
        'table' => $endpoint->table_id,
        'field' => $this->rel_1_field_name,
        'relationship' => $this->rel_1,
      ),
    );
    $view->display['default']->handler->override_option('fields', array_merge($existing_fields, $new_fields));

    // Execute the view.
    $view->execute();

    // Test that the right result was returned.
    $this->assertTrue($view->result[0]->{$this->rel_1_field_name} === 'SPARQL Views: A Visual SPARQL Query Builder for Drupal', t('Relationship query returns correct result.'));
  }

  /**
   * Test whether the name field can be retrieved from two people, related
   * resources of the same type. This can pose a problem because the query
   * could potentially end up looking for two related people with the same
   * name.
   */
  public function testRelationshipDuplicatedFieldQuery() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    $view->display['default']->handler->override_option('relationships', array(
      $this->rel_knows => array(
        'id' => $this->rel_knows,
        'table' => $endpoint->table_id,
        'field' => $this->rel_knows,
        'relationship' => 'none',
      ),
    ));

    $existing_fields = $view->display['default']->handler->get_option('fields');
    $new_fields = array(
      'field_name' => array(
        'id' => $this->field_name,
        'table' => $endpoint->table_id,
        'field' => $this->field_name,
        'relationship' => $this->rel_knows,
      ),
    );
    $view->display['default']->handler->override_option('fields', array_merge($existing_fields, $new_fields));

    // Execute the view.
    $view->execute();

    // Test that the right result was returned. Add _1 to the end of the field
    // name because that is how it would be aliased.
    $this->assertTrue(count($view->result) == 3 && $view->result[0]->{$this->field_name . '_1'} === 'Vinny Reynolds', t('Relationship query using same field on related resource returns correct result.'));
  }

  /**
   * Test simple argument.
   */
  public function testSimpleArgument() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    // Add a argument.
    $view->display['default']->handler->override_option('arguments', array(
      $this->field_name => array(
        'default_action' => 'ignore',
        'id' => $this->field_name,
        'table' => $endpoint->table_id,
        'field' => $this->field_name,
        'relationship' => 'none',
        'sparql_options' => array(
          'value_type' => 'string',
          'language' => '0',
          'code' => '',
        ),
      )
    ));

    $saved_view = clone $view;

    // Execute with a view
    $view->set_arguments(array('Lin Clark'));
    $view->execute();

    $this->assertTrue(count($view->result) == 1 && $view->result[0]->{$this->main_resource} === 'http://data.semanticweb.org/person/lin-clark', t('Argument query returns correct result.'));
  }

  /**
   * Test complex relationship + argument.
   */
  public function testRelationshipArgument() {
    $view = $this->getBasicView();
    $endpoint = sparql_views_get_endpoint($this->endpoint['uri']);

    // Add extra field.
    $existing_fields = $view->display['default']->handler->get_option('fields');
    $new_fields = array(
      // Won't have to add the field once #1104074 is fixed.
      'rel 1' => array(
        'id' => $this->rel_1,
        'table' => $endpoint->table_id,
        'field' => $this->rel_1,
        'relationship' => 'none',
      ),
    );
    $view->display['default']->handler->override_option('fields', array_merge($existing_fields, $new_fields));
    // Add a argument.
    $view->display['default']->handler->override_option('arguments', array(
      $this->field_name => array(
        'default_action' => 'ignore',
        'id' => $this->rel_1_field_name,
        'table' => $endpoint->table_id,
        'field' => $this->rel_1_field_name,
        'relationship' => $this->rel_1,
        'sparql_options' => array(
          'value_type' => 'string',
          'language' => '0',
          'code' => '',
        ),
      )
    ));

    $saved_view = clone $view;

    // Execute with a view
    $view->set_arguments(array('SPARQL Views: A Visual SPARQL Query Builder for Drupal'));
    $view->execute();

    $this->assertTrue(count($view->result) == 1 && $view->result[0]->{$this->rel_1} === 'http://data.semanticweb.org/conference/iswc/2010/paper/518', t('Argument/relationship query returns correct result.'));
  }
}