diff --git a/code/solr/reindex/handlers/SolrReindexBase.php b/code/solr/reindex/handlers/SolrReindexBase.php index 5c67f35..f127537 100644 --- a/code/solr/reindex/handlers/SolrReindexBase.php +++ b/code/solr/reindex/handlers/SolrReindexBase.php @@ -1,6 +1,10 @@ setField('id', $id); $document->setField('title', "Item $id"); return $document; @@ -60,16 +63,3 @@ class Solr4ServiceTest extends SapphireTest ); } } - -class Solr4ServiceTest_RecordingService extends Solr4Service_Core -{ - protected function _sendRawPost($url, $rawPost, $timeout = false, $contentType = 'text/xml; charset=UTF-8') - { - return $rawPost; - } - - protected function _sendRawGet($url, $timeout = false) - { - return $url; - } -} diff --git a/tests/Solr4ServiceTest/Solr4ServiceTest_RecordingService.php b/tests/Solr4ServiceTest/Solr4ServiceTest_RecordingService.php new file mode 100644 index 0000000..3fd0adc --- /dev/null +++ b/tests/Solr4ServiceTest/Solr4ServiceTest_RecordingService.php @@ -0,0 +1,19 @@ +addClass('File'); - $this->addClass('SiteTree'); - $this->addAllFulltextFields(); - } -} diff --git a/tests/SolrIndexSubsitesTest.yml b/tests/SolrIndexSubsitesTest/SolrIndexSubsitesTest.yml similarity index 100% rename from tests/SolrIndexSubsitesTest.yml rename to tests/SolrIndexSubsitesTest/SolrIndexSubsitesTest.yml diff --git a/tests/SolrIndexSubsitesTest/SolrIndexSubsitesTest_Index.php b/tests/SolrIndexSubsitesTest/SolrIndexSubsitesTest_Index.php new file mode 100644 index 0000000..97e9dfb --- /dev/null +++ b/tests/SolrIndexSubsitesTest/SolrIndexSubsitesTest_Index.php @@ -0,0 +1,15 @@ +addClass('File'); + $this->addClass('SiteTree'); + $this->addAllFulltextFields(); + } +} diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php index e75b8e5..0a6b82f 100644 --- a/tests/SolrIndexTest.php +++ b/tests/SolrIndexTest.php @@ -1,6 +1,10 @@ addClass('SearchUpdaterTest_Container'); - - $this->addFilterField('Field1'); - $this->addFilterField('MyDate', 'Date'); - $this->addFilterField('HasOneObject.Field1'); - $this->addFilterField('HasManyObjects.Field1'); - $this->addFilterField('ManyManyObjects.Field1'); - } -} - - -class SolrIndexTest_FakeIndex2 extends SolrIndex -{ - protected function getStoredDefault() - { - // Override isDev defaulting to stored - return 'false'; - } - - public function init() - { - $this->addClass('SearchUpdaterTest_Container'); - $this->addFilterField('MyDate', 'Date'); - $this->addFilterField('HasOneObject.Field1'); - $this->addFilterField('HasManyObjects.Field1'); - $this->addFilterField('ManyManyObjects.Field1'); - } -} - - -class SolrIndexTest_BoostedIndex extends SolrIndex -{ - protected function getStoredDefault() - { - // Override isDev defaulting to stored - return 'false'; - } - - public function init() - { - $this->addClass('SearchUpdaterTest_Container'); - $this->addAllFulltextFields(); - $this->setFieldBoosting('SearchUpdaterTest_Container_Field1', 1.5); - $this->addBoostedField('Field2', null, array(), 2.1); - } -} diff --git a/tests/SolrIndexTest/SolrIndexTest_BoostedIndex.php b/tests/SolrIndexTest/SolrIndexTest_BoostedIndex.php new file mode 100644 index 0000000..65dae12 --- /dev/null +++ b/tests/SolrIndexTest/SolrIndexTest_BoostedIndex.php @@ -0,0 +1,22 @@ +addClass('SearchUpdaterTest_Container'); + $this->addAllFulltextFields(); + $this->setFieldBoosting('SearchUpdaterTest_Container_Field1', 1.5); + $this->addBoostedField('Field2', null, array(), 2.1); + } +} diff --git a/tests/SolrIndexTest/SolrIndexTest_FakeIndex.php b/tests/SolrIndexTest/SolrIndexTest_FakeIndex.php new file mode 100644 index 0000000..b04e6ce --- /dev/null +++ b/tests/SolrIndexTest/SolrIndexTest_FakeIndex.php @@ -0,0 +1,19 @@ +addClass('SearchUpdaterTest_Container'); + + $this->addFilterField('Field1'); + $this->addFilterField('MyDate', 'Date'); + $this->addFilterField('HasOneObject.Field1'); + $this->addFilterField('HasManyObjects.Field1'); + $this->addFilterField('ManyManyObjects.Field1'); + } +} diff --git a/tests/SolrIndexTest/SolrIndexTest_FakeIndex2.php b/tests/SolrIndexTest/SolrIndexTest_FakeIndex2.php new file mode 100644 index 0000000..a9b58ac --- /dev/null +++ b/tests/SolrIndexTest/SolrIndexTest_FakeIndex2.php @@ -0,0 +1,23 @@ +addClass('SearchUpdaterTest_Container'); + $this->addFilterField('MyDate', 'Date'); + $this->addFilterField('HasOneObject.Field1'); + $this->addFilterField('HasManyObjects.Field1'); + $this->addFilterField('ManyManyObjects.Field1'); + } +} diff --git a/tests/SolrIndexVersionedTest.php b/tests/SolrIndexVersionedTest.php index 2c06ea5..4533576 100644 --- a/tests/SolrIndexVersionedTest.php +++ b/tests/SolrIndexVersionedTest.php @@ -1,8 +1,10 @@ deleteById($this->getExpectedDocumentId($id, 'Live')); } } - - -class SolrVersionedTest_Index extends SolrIndex -{ - public function init() - { - $this->addClass('SearchVariantVersionedTest_Item'); - $this->addClass('SolrIndexVersionedTest_Object'); - $this->addFilterField('TestText'); - $this->addFulltextField('Content'); - } -} - -/** - * Non-sitetree versioned dataobject - */ -class SolrIndexVersionedTest_Object extends DataObject implements TestOnly { - - private static $extensions = array( - 'Versioned' - ); - - private static $db = array( - 'Title' => 'Varchar', - 'Content' => 'Text', - 'TestText' => 'Varchar', - ); -} - -if (!class_exists('Phockito')) { - return; -} - -class SolrDocumentMatcher extends Hamcrest_BaseMatcher -{ - protected $properties; - - public function __construct($properties) - { - $this->properties = $properties; - } - - public function describeTo(\Hamcrest_Description $description) - { - $description->appendText('Apache_Solr_Document with properties '.var_export($this->properties, true)); - } - - public function matches($item) - { - if (! ($item instanceof Apache_Solr_Document)) { - return false; - } - - foreach ($this->properties as $key => $value) { - if ($item->{$key} != $value) { - return false; - } - } - - return true; - } -} diff --git a/tests/SolrIndexVersionedTest/SolrDocumentMatcher.php b/tests/SolrIndexVersionedTest/SolrDocumentMatcher.php new file mode 100644 index 0000000..5c9298e --- /dev/null +++ b/tests/SolrIndexVersionedTest/SolrDocumentMatcher.php @@ -0,0 +1,39 @@ +properties = $properties; + } + + public function describeTo(\Hamcrest_Description $description) + { + $description->appendText('Apache_Solr_Document with properties '.var_export($this->properties, true)); + } + + public function matches($item) + { + if (! ($item instanceof Apache_Solr_Document)) { + return false; + } + + foreach ($this->properties as $key => $value) { + if ($item->{$key} != $value) { + return false; + } + } + + return true; + } +} diff --git a/tests/SolrIndexVersionedTest/SolrIndexVersionedTest_Object.php b/tests/SolrIndexVersionedTest/SolrIndexVersionedTest_Object.php new file mode 100644 index 0000000..919d573 --- /dev/null +++ b/tests/SolrIndexVersionedTest/SolrIndexVersionedTest_Object.php @@ -0,0 +1,22 @@ + 'Varchar', + 'Content' => 'Text', + 'TestText' => 'Varchar', + ); +} diff --git a/tests/SolrIndexVersionedTest/SolrVersionedTest_Index.php b/tests/SolrIndexVersionedTest/SolrVersionedTest_Index.php new file mode 100644 index 0000000..7ac3a5b --- /dev/null +++ b/tests/SolrIndexVersionedTest/SolrVersionedTest_Index.php @@ -0,0 +1,16 @@ +addClass('SearchVariantVersionedTest_Item'); + $this->addClass('SolrIndexVersionedTest_Object'); + $this->addFilterField('TestText'); + $this->addFulltextField('Content'); + } +} diff --git a/tests/SolrReindexTest.php b/tests/SolrReindexTest.php index 5f696fb..a740edf 100644 --- a/tests/SolrReindexTest.php +++ b/tests/SolrReindexTest.php @@ -1,13 +1,6 @@ getIndexName(); - $stateName = json_encode($state); - $logger->info("Called processGroup with {$indexName}, {$stateName}, {$class}, group {$group} of {$groups}"); - } - - public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null) - { - $logger->info("Called triggerReindex"); - } -} - - -class SolrReindexTest_Index extends SolrIndex implements TestOnly -{ - public function init() - { - $this->addClass('SolrReindexTest_Item'); - $this->addAllFulltextFields(); - } -} - -/** - * Does not have any variant extensions - */ -class SolrReindexTest_Item extends DataObject implements TestOnly -{ - private static $extensions = array( - 'SolrReindexTest_ItemExtension' - ); - - private static $db = array( - 'Title' => 'Varchar(255)', - 'Variant' => 'Int(0)' - ); -} - -/** - * Select only records in the current variant - */ -class SolrReindexTest_ItemExtension extends DataExtension implements TestOnly -{ - /** - * Filter records on the current variant - * - * @param SQLQuery $query - * @param DataQuery $dataQuery - */ - public function augmentSQL(SilverStripe\ORM\Queries\SQLSelect $query, SilverStripe\ORM\DataQuery $dataQuery = NULL) - { - $variant = SolrReindexTest_Variant::get_current(); - if ($variant !== null && !$query->filtersOnID()) { - $sqlVariant = Convert::raw2sql($variant); - $query->addWhere("\"Variant\" = '{$sqlVariant}'"); - } - } -} - - -/** - * Dummy variant that selects items with field Varient matching the current value - * - * Variant states are 0 and 1, or null if disabled - */ -class SolrReindexTest_Variant extends SearchVariant implements TestOnly -{ - /** - * Value of this variant (either null, 0, or 1) - * - * @var int|null - */ - protected static $current = null; - - /** - * Activate this variant - */ - public static function enable() - { - self::disable(); - - self::$current = 0; - self::$variants = array( - 'SolrReindexTest_Variant' => singleton('SolrReindexTest_Variant') - ); - } - - /** - * Disable this variant and reset - */ - public static function disable() - { - self::$current = null; - self::$variants = null; - self::$class_variants = array(); - self::$call_instances = array(); - } - - public function activateState($state) - { - self::set_current($state); - } - - /** - * Set the current variant to the given state - * - * @param int $current 0, 1, 2, or null (disabled) - */ - public static function set_current($current) - { - self::$current = $current; - } - - /** - * Get the current state - * - * @return string|null - */ - public static function get_current() - { - // Always use string values for states for consistent json_encode value - if (isset(self::$current)) { - return (string)self::$current; - } - } - - public function alterDefinition($class, $index) - { - $self = get_class($this); - - $this->addFilterField($index, '_testvariant', array( - 'name' => '_testvariant', - 'field' => '_testvariant', - 'fullfield' => '_testvariant', - 'base' => ClassInfo::baseDataClass($class), - 'origin' => $class, - 'type' => 'Int', - 'lookup_chain' => array(array('call' => 'variant', 'variant' => $self, 'method' => 'currentState')) - )); - } - - public function alterQuery($query, $index) - { - // I guess just calling it _testvariant is ok? - $query->filter('_testvariant', $this->currentState()); - } - - public function appliesTo($class, $includeSubclasses) - { - return $class === 'SolrReindexTest_Item' || - ($includeSubclasses && is_subclass_of($class, 'SolrReindexTest_Item', true)); - } - - public function appliesToEnvironment() - { - // Set to null to disable - return self::$current !== null; - } - - public function currentState() - { - return self::get_current(); - } - - public function reindexStates() - { - // Always use string values for states for consistent json_encode value - return array('0', '1', '2'); - } -} - -/** - * Test logger for recording messages - */ -class SolrReindexTest_RecordingLogger extends Logger implements TestOnly -{ - /** - * @var SolrReindexTest_Handler - */ - protected $testHandler = null; - - public function __construct($name = 'testlogger', array $handlers = array(), array $processors = array()) - { - parent::__construct($name, $handlers, $processors); - - $this->testHandler = new SolrReindexTest_Handler(); - $this->pushHandler($this->testHandler); - } - - /** - * @return array - */ - public function getMessages() - { - return $this->testHandler->getMessages(); - } - - /** - * Clear all messages - */ - public function clear() - { - $this->testHandler->clear(); - } - - /** - * Get messages with the given filter - * - * @param string $containing - * @return array Filtered array - */ - public function filterMessages($containing) - { - return array_values(array_filter( - $this->getMessages(), - function ($content) use ($containing) { - return stripos($content, $containing) !== false; - } - )); - } - - /** - * Count all messages containing the given substring - * - * @param string $containing Message to filter by - * @return int - */ - public function countMessages($containing = null) - { - if ($containing) { - $messages = $this->filterMessages($containing); - } else { - $messages = $this->getMessages(); - } - return count($messages); - } -} - -/** - * Logger for recording messages for later retrieval - */ -class SolrReindexTest_Handler extends AbstractProcessingHandler implements TestOnly -{ - /** - * Messages - * - * @var array - */ - protected $messages = array(); - - /** - * Get all messages - * - * @return array - */ - public function getMessages() - { - return $this->messages; - } - - public function clear() - { - $this->messages = array(); - } - - protected function write(array $record) - { - $this->messages[] = $record['message']; - } -} diff --git a/tests/SolrReindexTest/SolrReindexTest_Handler.php b/tests/SolrReindexTest/SolrReindexTest_Handler.php new file mode 100644 index 0000000..ea29bcd --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_Handler.php @@ -0,0 +1,39 @@ +messages; + } + + public function clear() + { + $this->messages = array(); + } + + protected function write(array $record) + { + $this->messages[] = $record['message']; + } +} diff --git a/tests/SolrReindexTest/SolrReindexTest_Index.php b/tests/SolrReindexTest/SolrReindexTest_Index.php new file mode 100644 index 0000000..64e85b2 --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_Index.php @@ -0,0 +1,15 @@ +addClass('SolrReindexTest_Item'); + $this->addAllFulltextFields(); + } +} diff --git a/tests/SolrReindexTest/SolrReindexTest_Item.php b/tests/SolrReindexTest/SolrReindexTest_Item.php new file mode 100644 index 0000000..a77343a --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_Item.php @@ -0,0 +1,21 @@ + 'Varchar(255)', + 'Variant' => 'Int(0)' + ); +} diff --git a/tests/SolrReindexTest/SolrReindexTest_ItemExtension.php b/tests/SolrReindexTest/SolrReindexTest_ItemExtension.php new file mode 100644 index 0000000..8306d5d --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_ItemExtension.php @@ -0,0 +1,29 @@ +filtersOnID()) { + $sqlVariant = Convert::raw2sql($variant); + $query->addWhere("\"Variant\" = '{$sqlVariant}'"); + } + } +} diff --git a/tests/SolrReindexTest/SolrReindexTest_RecordingLogger.php b/tests/SolrReindexTest/SolrReindexTest_RecordingLogger.php new file mode 100644 index 0000000..3606e02 --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_RecordingLogger.php @@ -0,0 +1,74 @@ +testHandler = new SolrReindexTest_Handler(); + $this->pushHandler($this->testHandler); + } + + /** + * @return array + */ + public function getMessages() + { + return $this->testHandler->getMessages(); + } + + /** + * Clear all messages + */ + public function clear() + { + $this->testHandler->clear(); + } + + /** + * Get messages with the given filter + * + * @param string $containing + * @return array Filtered array + */ + public function filterMessages($containing) + { + return array_values(array_filter( + $this->getMessages(), + function ($content) use ($containing) { + return stripos($content, $containing) !== false; + } + )); + } + + /** + * Count all messages containing the given substring + * + * @param string $containing Message to filter by + * @return int + */ + public function countMessages($containing = null) + { + if ($containing) { + $messages = $this->filterMessages($containing); + } else { + $messages = $this->getMessages(); + } + return count($messages); + } +} diff --git a/tests/SolrReindexTest/SolrReindexTest_TestHandler.php b/tests/SolrReindexTest/SolrReindexTest_TestHandler.php new file mode 100644 index 0000000..fd86b88 --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_TestHandler.php @@ -0,0 +1,26 @@ +getIndexName(); + $stateName = json_encode($state); + $logger->info("Called processGroup with {$indexName}, {$stateName}, {$class}, group {$group} of {$groups}"); + } + + public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null) + { + $logger->info("Called triggerReindex"); + } +} diff --git a/tests/SolrReindexTest/SolrReindexTest_Variant.php b/tests/SolrReindexTest/SolrReindexTest_Variant.php new file mode 100644 index 0000000..038c4cc --- /dev/null +++ b/tests/SolrReindexTest/SolrReindexTest_Variant.php @@ -0,0 +1,117 @@ + singleton('SolrReindexTest_Variant') + ); + } + + /** + * Disable this variant and reset + */ + public static function disable() + { + self::$current = null; + self::$variants = null; + self::$class_variants = array(); + self::$call_instances = array(); + } + + public function activateState($state) + { + self::set_current($state); + } + + /** + * Set the current variant to the given state + * + * @param int $current 0, 1, 2, or null (disabled) + */ + public static function set_current($current) + { + self::$current = $current; + } + + /** + * Get the current state + * + * @return string|null + */ + public static function get_current() + { + // Always use string values for states for consistent json_encode value + if (isset(self::$current)) { + return (string)self::$current; + } + } + + public function alterDefinition($class, $index) + { + $self = get_class($this); + + $this->addFilterField($index, '_testvariant', array( + 'name' => '_testvariant', + 'field' => '_testvariant', + 'fullfield' => '_testvariant', + 'base' => ClassInfo::baseDataClass($class), + 'origin' => $class, + 'type' => 'Int', + 'lookup_chain' => array(array('call' => 'variant', 'variant' => $self, 'method' => 'currentState')) + )); + } + + public function alterQuery($query, $index) + { + // I guess just calling it _testvariant is ok? + $query->filter('_testvariant', $this->currentState()); + } + + public function appliesTo($class, $includeSubclasses) + { + return $class === 'SolrReindexTest_Item' || + ($includeSubclasses && is_subclass_of($class, 'SolrReindexTest_Item', true)); + } + + public function appliesToEnvironment() + { + // Set to null to disable + return self::$current !== null; + } + + public function currentState() + { + return self::get_current(); + } + + public function reindexStates() + { + // Always use string values for states for consistent json_encode value + return array('0', '1', '2'); + } +}