Commit 646588d8 authored by Jur de Vries's avatar Jur de Vries

Created more tests.

parent eaebaf37
......@@ -13,6 +13,7 @@ php:
notifications:
email:
- nick.veenhof@acquia.com
- jur@triquanta.nl
irc:
- "chat.freenode.net#drupal-search-api"
......
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="Facetapi Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<!-- Filter for coverage reports. -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
<exclude>
<directory>./src/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
......@@ -393,7 +393,7 @@ interface AdapterInterface {
* @param array $facet
* The facet definition as returned by facet_api_facet_load().
*
* @return
* @return \stdClass
* An object containing the settings.
*
* @see ctools_export_crud_load()
......
......@@ -17,7 +17,7 @@ namespace Drupal\facet_api;
*
* @TODO: Remove ArrayAccess dependency... 'Cause we like object!
*/
class Facet implements ArrayAccess {
class Facet implements \ArrayAccess {
/**
* The FacetapiAdapter object this class was instantiated from.
......
......@@ -8,7 +8,7 @@
namespace Drupal\facet_api\Plugin\Url;
use Drupal\facet_api\Plugin\Adapter\AdapterInterface;
use Drupal\facet_api\Adapter\AdapterInterface;
/**
* In D7 this was the abstract class extended by all url processor plugins.
......@@ -33,7 +33,7 @@ abstract class FacetUrlProcessor {
/**
* Held the adapter that the url processor plugin was associated with.
*
* @var AdapterInterface
* @var \Drupal\facet_api\Adapter\AdapterInterface
*/
protected $adapter;
......
......@@ -74,37 +74,37 @@ class FacetUrlProcessorStandard extends FacetUrlProcessor {
* The query string variables.
*/
public function getQueryString(array $facet, array $values, $active) {
$qstring = $this->params;
$query_string = $this->params;
$active_items = $this->adapter->getActiveItems($facet);
// Appends to qstring if inactive, removes if active.
foreach ($values as $value) {
if ($active && isset($active_items[$value])) {
unset($qstring[$this->filterKey][$active_items[$value]['pos']]);
unset($query_string[$this->filterKey][$active_items[$value]['pos']]);
}
elseif (!$active) {
$field_alias = rawurlencode($facet['field alias']);
// Strips all other filters for this facet if limit option is set.
if ($this->limitActiveItems($facet)) {
foreach ($qstring[$this->filterKey] as $pos => $filter) {
foreach ($query_string[$this->filterKey] as $pos => $filter) {
// Refactor the if statement to best practises?
// (strpos($filter, $field_alias) === 0) or (strpos($filter, $field_alias) === FALSE)
if (0 === strpos($filter, $field_alias)) {
unset($qstring[$this->filterKey][$pos]);
unset($query_string[$this->filterKey][$pos]);
}
}
}
// Adds the filter to the query string.
$qstring[$this->filterKey][] = $field_alias . ':' . $value;
$query_string[$this->filterKey][] = $field_alias . ':' . $value;
}
}
// Removes duplicates, resets array keys and returns query string.
// @see http://drupal.org/node/1340528
$qstring[$this->filterKey] = array_values(array_unique($qstring[$this->filterKey]));
return array_filter($qstring);
$query_string[$this->filterKey] = array_values(array_unique($query_string[$this->filterKey]));
return array_filter($query_string);
}
/**
......
......@@ -7,7 +7,7 @@
namespace Drupal\facet_api\Searcher;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines the interface for searchers.
......
......@@ -7,7 +7,7 @@
namespace Drupal\facet_api\Searcher;
use Drupal\Component\Plugin\PluginBase;
use Drupal\facet_api\Searcher\SearcherInterface
use Drupal\facet_api\Searcher\SearcherInterface;
class SearcherPluginBase extends PluginBase implements SearcherInterface {
......
......@@ -7,7 +7,7 @@
namespace Drupal\facet_api\Sort;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines the interface for image effects.
......
<?php
/**
* @file
* Contains \Drupal\Tests\facet_api\Plugin\Url\FacetUrlProcessorStandardTest.
*/
namespace Drupal\Tests\facet_api\Plugin\Adapter;
use Drupal\facet_api\Plugin\Adapter\AdapterBase;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard
*
* @group facet_api
*
*/
class AdapterBaseTest extends UnitTestCase {
/**
* Stores the processor which is not tested here.
*
* @var \Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard|\PHPUnit_Framework_MockObject_MockObject
*/
protected $processor;
/**
* Stores the adapter under test.
*
* @var \Drupal\facet_api\Adapter\AdapterInterface
*/
protected $adapter;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// @TODO: implement the setup.
// // Create a mock for the URL to be returned.
// $this->adapter = $this->getMock('Drupal\facet_api\Adapter\AdapterInterface');
// Create the URL-Processor and set the mocked indexer.
// $this->processor = $this->getMock('Drupal\facet_api\Ur');
}
public function testGetPageTotal() {
// @TODO: implement more tests.
}
}
......@@ -10,44 +10,41 @@ use Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard;
use Drupal\Tests\UnitTestCase;
/**
* Tests the "URL field" processor.
* @coversDefaultClass \Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard
*
* @group facet_api
*
* @see \Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard
*/
class FacetUrlProcessorStandardTest extends UnitTestCase {
/**
* Stores the processor to be tested.
* Stores the processor under test.
*
* @var \Drupal\facet_api\Plugin\Url\FacetUrlProcessorStandard
*/
protected $processor;
/**
* Stores the processor to be tested.
* Stores the adapter which is not tested here.
*
* @var \Drupal\facet_api\Adapter\AdapterInterface
* @var \Drupal\facet_api\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $adapter;
/**
* Creates a new processor object for use in the tests.
* {@inheritdoc}
*/
protected function setUp() {
public function setUp() {
parent::setUp();
// Create a mock for the URL to be returned.
$this->adapter = $this->getMock('Drupal\facet_api\Plugin\Adapter\AdapterInterface');
$this->adapter = $this->getMock('Drupal\facet_api\Adapter\AdapterInterface');
// Create the URL-Processor and set the mocked indexer.
$this->processor = new FacetUrlProcessorStandard($this->adapter);
}
/**
* Tests processIndexItems.
*
* Check if the items are processed as expected.
* @covers ::normalizeParams
*/
public function testNormalizeParams() {
// Process the items.
......@@ -58,4 +55,89 @@ class FacetUrlProcessorStandardTest extends UnitTestCase {
$this->assertArrayHasKey('foo', $normalized_param, 'foo parameter is still there');
$this->assertArrayHasKey('bar', $normalized_param, 'bar parameter is still there');
}
/**
* @covers ::getQueryString
*
* @dataProvider providerTestQueryString
*/
public function testGetQueryString($active, $value_item) {
$facet = array(
'field alias' => $this->randomMachineName(),
'name' => $this->randomMachineName(),
);
$values = array($this->randomMachineName(), $value_item);
$active_items = array(
'foo' => array(
'field alias' => $this->randomMachineName(),
'value' => array($this->randomMachineName()),
'pos' => mt_rand(0,2),
),
);
$this->adapter->expects($this->atLeastOnce())
->method('getActiveItems')
->with($facet)
->willReturn($active_items);
$settings = new \stdClass();
$settings->settings = array(
'limit_active_items' => (bool) mt_rand(0, 1),
);
$this->adapter->expects($active == 0 ? $this->atLeastOnce() : $this->never())
->method('getFacetSettingsGlobal')
->with($facet)
->willReturn($settings);
$this->processor->setParams(array(
$this->processor->getFilterKey() => array(
$this->randomMachineName(),
$this->randomMachineName(),
$this->randomMachineName(),
),
));
$this->processor->getQueryString($facet, $values, $active);
}
/**
* @covers ::limitActiveItems
*/
public function testLimitActiveItems() {
$facet = array(
'field alias' => $this->randomMachineName(),
'name' => $this->randomMachineName(),
);
$settings = new \stdClass();
$settings->settings = array(
'limit_active_items' => (bool) mt_rand(0, 1),
);
$this->adapter->expects($this->atLeastOnce())
->method('getFacetSettingsGlobal')
->with($facet)
->willReturn($settings);
$limit_active_items = $this->processor->limitActiveItems($facet);
$this->assertEquals($settings->settings['limit_active_items'], $limit_active_items);
}
/**
* Provides data for self::testGetQueryString().
*
* @return array
*/
public function providerTestQueryString() {
return array(
array(0, 'foo'),
array(1, 'foo'),
array(0, 'bar'),
array(1, 'bar'),
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment