Skip to content

Commit

Permalink
NEW Add config for shutdown flush, remove references to Object and up…
Browse files Browse the repository at this point in the history
…date tests
  • Loading branch information
robbieaverill committed Nov 14, 2017
1 parent 0de6f52 commit b448bfc
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 54 deletions.
14 changes: 9 additions & 5 deletions code/search/indexes/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use InvalidArgumentException;
use SilverStripe\View\ViewableData;
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\Object;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\FullTextSearch\Search\SearchIntrospection;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
use SilverStripe\FullTextSearch\Utils\MultipleArrayIterator;
Expand Down Expand Up @@ -160,11 +160,15 @@ public function fieldData($field, $forceType = null, $extraOptions = array())
continue;
}

$class = $manyMany[2];
$class = $manyMany['childClass'];
$options['multi_valued'] = true;
$options['lookup_chain'][] = array(
'call' => 'method', 'method' => $lookup,
'through' => 'many_many', 'class' => $dataclass, 'otherclass' => $class, 'details' => $manyMany
'call' => 'method',
'method' => $lookup,
'through' => 'many_many',
'class' => $dataclass,
'otherclass' => $class,
'details' => $manyMany,
);
}

Expand Down Expand Up @@ -357,7 +361,7 @@ public function addAllFulltextFields($includeSubclasses = true)
if (preg_match('/^(\w+)\(/', $type, $match)) {
$type = $match[1];
}
list($type, $args) = Object::parse_class_spec($type);
list($type, $args) = ClassInfo::parse_class_spec($type);

// Get class from shortName
$object = Injector::inst()->get($type, false, ['Name' => 'test']);
Expand Down
18 changes: 12 additions & 6 deletions code/search/updaters/SearchUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace SilverStripe\FullTextSearch\Search\Updaters;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Object;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
Expand All @@ -28,6 +28,16 @@

class SearchUpdater
{
use Configurable;

/**
* Whether to register the shutdown function to flush. Can be disabled for example in unit testing.
*
* @config
* @var bool
*/
private static $flush_on_shutdown = true;

/**
* Replace the database object with a subclass that captures all manipulations and passes them to us
*/
Expand Down Expand Up @@ -174,11 +184,7 @@ public static function process_writes($writes)
}

// If we do have some work to do register the shutdown function to actually do the work

// Don't do it if we're testing - there's no database connection outside the test methods, so we'd
// just get errors

if (self::$processor && !self::$registered && !SapphireTest::is_running_test()) {
if (self::$processor && !self::$registered && self::config()->get('flush_on_shutdown')) {
register_shutdown_function(array(SearchUpdater::class, "flush_dirty_indexes"));
self::$registered = true;
}
Expand Down
3 changes: 2 additions & 1 deletion code/solr/reindex/handlers/SolrReindexBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\FullTextSearch\Solr\Reindex\Handlers;

use Psr\Log\LoggerInterface;
use SilverStripe\Core\Environment;
use SilverStripe\FullTextSearch\Solr\Solr;
use SilverStripe\FullTextSearch\Solr\SolrIndex;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
Expand Down Expand Up @@ -146,7 +147,7 @@ public function runGroup(
LoggerInterface $logger, SolrIndex $indexInstance, $state, $class, $groups, $group
) {
// Set time limit and state
increase_time_limit_to();
Environment::increaseTimeLimitTo();
SearchVariant::activate_state($state);
$logger->info("Adding $class");

Expand Down
4 changes: 3 additions & 1 deletion tests/SearchUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class SearchUpdaterTest extends SapphireTest

private static $index = null;

public function setUp()
protected function setUp()
{
Config::modify()->set(SearchUpdater::class, 'flush_on_shutdown', false);

parent::setUp();

if (self::$index === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/
class SearchUpdaterTest_ExtendedContainer extends SearchUpdaterTest_OtherContainer
{
private static $db = array(
private static $table_name = 'SearchUpdaterTest_ExtendedContainer';

private static $db = [
'SomeField' => 'Varchar',
);
];
}
2 changes: 2 additions & 0 deletions tests/SearchUpdaterTest/SearchUpdaterTest_OtherContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class SearchUpdaterTest_OtherContainer extends DataObject
{
private static $table_name = 'SearchUpdaterTest_OtherContainer';

private static $has_many = [
'HasManyObjects' => SearchUpdaterTest_HasMany::class,
];
Expand Down
6 changes: 4 additions & 2 deletions tests/SearchVariantVersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class SearchVariantVersionedTest extends SapphireTest
SearchVariantVersionedTest_Item::class
);

public function setUp()
protected function setUp()
{
Config::modify()->set(SearchUpdater::class, 'flush_on_shutdown', false);

parent::setUp();

if (self::$index === null) {
Expand All @@ -34,7 +36,7 @@ public function setUp()

SearchUpdater::bind_manipulation_capture();

Config::modify()->set('Injector', SearchUpdateProcessor::class, array(
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
'class' => SearchUpdateImmediateProcessor::class
));

Expand Down
21 changes: 7 additions & 14 deletions tests/SolrIndexSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* Subsite specific solr testing
*/
class SolrIndexSubsitesTest extends SapphireTest {

public static $fixture_file = 'SolrIndexSubsitesTest/SolrIndexSubsitesTest.yml';
// @todo
// protected static $fixture_file = 'SolrIndexSubsitesTest/SolrIndexSubsitesTest.yml';

/**
* @var SolrIndexSubsitesTest_Index
Expand All @@ -21,16 +21,16 @@ class SolrIndexSubsitesTest extends SapphireTest {

protected $server = null;

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

// Prevent parent::setUp() crashing on db build
if (!class_exists('Subsite')) {
$this->skipTest = true;
$this->markTestSkipped("These tests need the Subsite module installed to run");
}

parent::setUp();

$this->server = $_SERVER;

if (!class_exists('\Phockito')) {
Expand All @@ -39,13 +39,6 @@ public function setUp()
return;
}

// Check versioned available
if (!class_exists('Subsite')) {
$this->skipTest = true;
$this->markTestSkipped('The subsite module is not installed');
return;
}

if (self::$index === null) {
self::$index = singleton('SolrIndexSubsitesTest_Index');
}
Expand All @@ -60,9 +53,9 @@ public function setUp()
SearchUpdater::clear_dirty_indexes();
}

public function tearDown()
protected function tearDown()
{
if($this->server) {
if ($this->server) {
$_SERVER = $this->server;
$this->server = null;
}
Expand Down
35 changes: 16 additions & 19 deletions tests/SolrIndexTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\FullTextSearch\Tests\SolrIndexTest\SolrIndexTest_AmbiguousRelationIndex;
use SilverStripe\FullTextSearch\Tests\SolrIndexTest\SolrIndexTest_AmbiguousRelationInheritedIndex;
Expand All @@ -13,10 +15,8 @@
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_ManyMany;
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_OtherContainer;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;


class SolrIndexTest extends SapphireTest
{
public function testFieldDataHasOne()
Expand Down Expand Up @@ -58,15 +58,15 @@ public function testFieldDataAmbiguousHasMany()
$index = new SolrIndexTest_AmbiguousRelationIndex();
$data = $index->fieldData('HasManyObjects.Field1');

$this->assertArrayHasKey('SearchUpdaterTest_Container_HasManyObjects_Field1', $data);
$this->assertArrayHasKey('SearchUpdaterTest_OtherContainer_HasManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_Container::class . '_HasManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_OtherContainer::class . '_HasManyObjects_Field1', $data);

$dataContainer = $data['SearchUpdaterTest_Container_HasManyObjects_Field1'];
$dataContainer = $data[SearchUpdaterTest_Container::class . '_HasManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['origin']);
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['base']);
$this->assertEquals(SearchUpdaterTest_HasMany::class, $dataContainer['class']);

$dataOtherContainer = $data['SearchUpdaterTest_OtherContainer_HasManyObjects_Field1'];
$dataOtherContainer = $data[SearchUpdaterTest_OtherContainer::class . '_HasManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['origin']);
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['base']);
$this->assertEquals(SearchUpdaterTest_HasMany::class, $dataOtherContainer['class']);
Expand All @@ -77,15 +77,15 @@ public function testFieldDataAmbiguousManyMany()
$index = new SolrIndexTest_AmbiguousRelationIndex();
$data = $index->fieldData('ManyManyObjects.Field1');

$this->assertArrayHasKey('SearchUpdaterTest_Container_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey('SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_Container::class . '_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_OtherContainer::class . '_ManyManyObjects_Field1', $data);

$dataContainer = $data['SearchUpdaterTest_Container_ManyManyObjects_Field1'];
$dataContainer = $data[SearchUpdaterTest_Container::class . '_ManyManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['origin']);
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['base']);
$this->assertEquals(SearchUpdaterTest_ManyMany::class, $dataContainer['class']);

$dataOtherContainer = $data['SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1'];
$dataOtherContainer = $data[SearchUpdaterTest_OtherContainer::class . '_ManyManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['origin']);
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['base']);
$this->assertEquals(SearchUpdaterTest_ManyMany::class, $dataOtherContainer['class']);
Expand All @@ -96,16 +96,16 @@ public function testFieldDataAmbiguousManyManyInherited()
$index = new SolrIndexTest_AmbiguousRelationInheritedIndex();
$data = $index->fieldData('ManyManyObjects.Field1');

$this->assertArrayHasKey('SearchUpdaterTest_Container_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey('SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1', $data);
$this->assertArrayNotHasKey('SearchUpdaterTest_ExtendedContainer_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_Container::class . '_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey(SearchUpdaterTest_OtherContainer::class . '_ManyManyObjects_Field1', $data);
$this->assertArrayNotHasKey(SearchUpdaterTest_ExtendedContainer::class . '_ManyManyObjects_Field1', $data);

$dataContainer = $data['SearchUpdaterTest_Container_ManyManyObjects_Field1'];
$dataContainer = $data[SearchUpdaterTest_Container::class . '_ManyManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['origin']);
$this->assertEquals(SearchUpdaterTest_Container::class, $dataContainer['base']);
$this->assertEquals(SearchUpdaterTest_ManyMany::class, $dataContainer['class']);

$dataOtherContainer = $data['SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1'];
$dataOtherContainer = $data[SearchUpdaterTest_OtherContainer::class . '_ManyManyObjects_Field1'];
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['origin']);
$this->assertEquals(SearchUpdaterTest_OtherContainer::class, $dataOtherContainer['base']);
$this->assertEquals(SearchUpdaterTest_ManyMany::class, $dataOtherContainer['class']);
Expand Down Expand Up @@ -248,8 +248,7 @@ public function testIndexExcludesNullValues()

public function testAddFieldExtraOptions()
{
Config::nest();
Director::set_environment_type('live');
Injector::inst()->get(Kernel::class)->setEnvironment('live');

$index = new SolrIndexTest_FakeIndex();

Expand All @@ -261,8 +260,6 @@ public function testAddFieldExtraOptions()
$defs = simplexml_load_string('<fields>' . $index->getFieldDefinitions() . '</fields>');
$defField1 = $defs->xpath('field[@name="' . SearchUpdaterTest_Container::class . '_Field1"]');
$this->assertEquals((string)$defField1[0]['stored'], 'true');

Config::unnest();
}

public function testAddAnalyzer()
Expand Down
6 changes: 4 additions & 2 deletions tests/SolrIndexVersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class SolrIndexVersionedTest extends SapphireTest
SolrIndexVersionedTest_Object::class
);

public function setUp()
protected function setUp()
{
Config::modify()->set(SearchUpdater::class, 'flush_on_shutdown', false);

parent::setUp();

if (self::$index === null) {
Expand All @@ -37,7 +39,7 @@ public function setUp()

SearchUpdater::bind_manipulation_capture();

Config::modify()->set('Injector', SearchUpdateProcessor::class, array(
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
'class' => SearchUpdateImmediateProcessor::class
));

Expand Down
9 changes: 7 additions & 2 deletions tests/SolrReindexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use SilverStripe\Dev\SapphireTest;
use SilverStripe\FullTextSearch\Search\FullTextSearch;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
use SilverStripe\FullTextSearch\Tests\SolrReindexTest\SolrReindexTest_Variant;
use SilverStripe\FullTextSearch\Tests\SolrReindexTest\SolrReindexTest_Index;
Expand Down Expand Up @@ -38,14 +39,17 @@ class SolrReindexTest extends SapphireTest
*/
protected $service = null;

public function setUp()
protected function setUp()
{
Config::modify()->set(SearchUpdater::class, 'flush_on_shutdown', false);

parent::setUp();

// Set test handler for reindex
Config::modify()->set('Injector', SolrReindexHandler::class, array(
Config::modify()->set(Injector::class, SolrReindexHandler::class, array(
'class' => SolrReindexTest_TestHandler::class
));

Injector::inst()->registerService(new SolrReindexTest_TestHandler(), SolrReindexHandler::class);

// Set test variant
Expand All @@ -55,6 +59,7 @@ public function setUp()
$this->service = $this->getServiceMock();
$this->index = singleton(SolrReindexTest_Index::class);
$this->index->setService($this->service);

FullTextSearch::force_index_list($this->index);
}

Expand Down

0 comments on commit b448bfc

Please sign in to comment.