You are here

function SolrBaseQueryTests::testAddParams in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 tests/solr_base_query.test \SolrBaseQueryTests::testAddParams()
  2. 7 tests/solr_base_query.test \SolrBaseQueryTests::testAddParams()

File

tests/solr_base_query.test, line 90

Class

SolrBaseQueryTests
Unit tests for query object methods.

Code

function testAddParams() {
  $examples = array();
  $examples['{!cache=false}inStock:true'] = array(
    '#local' => 'cache=false',
    '#exclude' => FALSE,
    '#name' => 'inStock',
    '#value' => 'true',
  );
  $examples['{!frange l=1 u=4 cache=false}sqrt(popularity)'] = array(
    '#local' => 'frange l=1 u=4 cache=false',
    '#exclude' => FALSE,
    '#name' => NULL,
    '#value' => 'sqrt(popularity)',
  );
  $examples['{!cache=false cost=5}inStock:true'] = array(
    '#local' => 'cache=false cost=5',
    '#exclude' => FALSE,
    '#name' => 'inStock',
    '#value' => 'true',
  );
  $examples['{!tag=impala}model:Impala'] = array(
    '#local' => 'tag=impala',
    '#exclude' => FALSE,
    '#name' => 'model',
    '#value' => 'Impala',
  );
  $examples['{!anything that appears to be local}'] = array(
    '#local' => 'anything that appears to be local',
    '#exclude' => FALSE,
    '#name' => NULL,
    '#value' => NULL,
  );
  $examples['bundle:(article OR page)'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'bundle',
    '#value' => '(article OR page)',
  );
  $examples['-bundle:(article OR page)'] = array(
    '#local' => NULL,
    '#exclude' => TRUE,
    '#name' => 'bundle',
    '#value' => '(article OR page)',
  );
  $examples['-{!anything that appears to be local}'] = array(
    '#local' => 'anything that appears to be local',
    '#exclude' => TRUE,
    '#name' => NULL,
    '#value' => NULL,
  );
  $examples['title:"double words"'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'title',
    '#value' => '"double words"',
  );
  $examples['field_date:[1970-12-31T23:59:59Z TO NOW]'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'field_date',
    '#value' => '[1970-12-31T23:59:59Z TO NOW]',
  );
  $query = $this
    ->_apachesolr_drupal_query("apachesolr_tests");
  foreach ($examples as $fq => $example) {
    $name = !empty($example['#name']) ? $example['#name'] : '_QUERY_';
    $value = !empty($example['#value']) ? $example['#value'] : '_VALUE_';
    $filter = $name . ':' . $value;

    // Check if filter is seen as a valid one
    $message = t('Filter (@fq) is Valid', array(
      '@fq' => $filter,
    ));
    $this
      ->assertTrue($query
      ->validFilterValue($filter), $message);
    $query
      ->addParam('fq', $fq);

    // Check if filter was added
    $message = t('hasFilter(@name, @value) is True', array(
      '@name' => $example['#name'],
      '@value' => $example['#value'],
    ));
    $this
      ->assertTrue($query
      ->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);
    $filters = $query
      ->getFilters();
    $filter = reset($filters);
    $message = t('The filter "@fq" was added with all the given properties', array(
      '@fq' => $fq,
    ));
    $this
      ->assertTrue(!array_diff($example, $filter), $message);
    $query
      ->removeFilter($example['#name']);
    $message = t('The filter "@fq" was correctly removed', array(
      '@fq' => $fq,
    ));
    $this
      ->assertFalse($query
      ->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);

    // Since we cannot remove filters without names yet we have to clear the whole fq array
    $query
      ->removeParam('fq');

    // Check the ones without the name also
  }

  // Check for invalid combinations
  $bad_examples['wrong name:"double words"'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'wrong name',
    '#value' => '"double words"',
  );
  $bad_examples['field_date:[1970-12-31 TO NOW]'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'field_date',
    '#value' => '[1970-12-31 TO NOW]',
  );
  $bad_examples['bundle:((article OR page)]'] = array(
    '#local' => NULL,
    '#exclude' => FALSE,
    '#name' => 'bundle',
    '#value' => '((article OR page)]',
  );
  foreach ($bad_examples as $fq => $example) {
    $name = !empty($example['#name']) ? $example['#name'] : '_QUERY_';
    $value = !empty($example['#value']) ? $example['#value'] : '_VALUE_';
    $filter = $name . ':' . $value;

    // Check if filter is seen as a valid one
    $message = t('Filter (@fq) is not Valid', array(
      '@fq' => $filter,
    ));
    $this
      ->assertFalse($query
      ->validFilterValue($filter), $message);
  }

  // Check parameter normalization.
  $query
    ->addParam('spellcheck', TRUE);
  $this
    ->assertTrue($query
    ->getParam('spellcheck') === 'true', "TRUE normalized to string 'true'");
  $query
    ->replaceParam('spellcheck', FALSE);
  $this
    ->assertTrue($query
    ->getParam('spellcheck') === 'false', "FALSE normalized to string 'false'");
  $query
    ->addParam('fl', array(
    ' x ',
    TRUE,
  ));
  $this
    ->assertTrue($query
    ->getParam('fl') === array(
    'x',
    'true',
  ), "Array of params all normalized");
}