Skip to content

Commit

Permalink
Merge pull request #132 from dhensby/pulls/2.2/fix-subsite-regression
Browse files Browse the repository at this point in the history
fix subsite regression
  • Loading branch information
dhensby authored Dec 14, 2016
2 parents 372bde9 + 465c072 commit b17bee7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
13 changes: 12 additions & 1 deletion code/search/SearchVariantSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ public function extractManipulationWriteState(&$writes)
continue;
}

if (isset($write['fields']['SiteTree:SubsiteID'])) {
$subsitesForWrite = array($write['fields']['SiteTree:SubsiteID']);
}
// files in subsite 0 should be in all subsites as they are global
elseif (isset($write['fields']['File:SubsiteID']) && intval($write['fields']['File:SubsiteID']) !== 0) {
$subsitesForWrite = array($write['fields']['File:SubsiteID']);
}
else {
$subsitesForWrite = $subsites;
}

$next = array();
foreach ($write['statefulids'] as $i => $statefulid) {
foreach ($subsites as $subsiteID) {
foreach ($subsitesForWrite as $subsiteID) {
$next[] = array(
'id' => $statefulid['id'],
'state' => array_merge(
Expand Down
110 changes: 110 additions & 0 deletions tests/SolrIndexSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,116 @@ public function testPublishing()
Phockito::verify($serviceMock)->addDocument($doc2);

}

public function testCorrectSubsiteIDOnPageWrite() {
$mockWrites = array(
'3367:SiteTree:a:1:{s:22:"SearchVariantVersioned";s:4:"Live";}' => array(
'base' => 'SiteTree',
'class' => 'Page',
'id' => 3367,
'statefulids' => array(
array(
'id' => 3367,
'state' => array(
'SearchVariantVersioned' => 'Live',
),
),
),
'fields' => array(
'SiteTree:ClassName' => 'Page',
'SiteTree:LastEdited' => '2016-12-08 23:55:30',
'SiteTree:Created' => '2016-11-30 05:23:58',
'SiteTree:URLSegment' => 'test',
'SiteTree:Title' => 'Test Title',
'SiteTree:Content' => '<p>test content</p>',
'SiteTree:MetaDescription' => 'a solr test',
'SiteTree:ShowInMenus' => 1,
'SiteTree:ShowInSearch' => 1,
'SiteTree:Sort' => 77,
'SiteTree:HasBrokenFile' => 0,
'SiteTree:HasBrokenLink' => 0,
'SiteTree:CanViewType' => 'Inherit',
'SiteTree:CanEditType' => 'Inherit',
'SiteTree:Locale' => 'en_NZ',
'SiteTree:SubsiteID' => 0,
'Page:ID' => 3367,
'Page:MetaKeywords' => null,
),
),
);
$variant = new SearchVariantSubsites();
$tmpMockWrites = $mockWrites;
$variant->extractManipulationWriteState($tmpMockWrites);
foreach($tmpMockWrites as $mockWrite) {
$this->assertCount(1, $mockWrite['statefulids']);
$statefulIDs = array_shift($mockWrite['statefulids']);
$this->assertEquals(0, $statefulIDs['state']['SearchVariantSubsites']);
}

$subsite = $this->objFromFixture('Subsite', 'subsite1');
$tmpMockWrites = $mockWrites;
$tmpMockWrites['3367:SiteTree:a:1:{s:22:"SearchVariantVersioned";s:4:"Live";}']['fields']['SiteTree:SubsiteID'] = $subsite->ID;

$variant->extractManipulationWriteState($tmpMockWrites);
foreach($tmpMockWrites as $mockWrite) {
$this->assertCount(1, $mockWrite['statefulids']);
$statefulIDs = array_shift($mockWrite['statefulids']);
$this->assertEquals($subsite->ID, $statefulIDs['state']['SearchVariantSubsites']);
}
}

public function testCorrectSubsiteIDOnFileWrite() {
$subsiteIDs = array('0') + $this->allFixtureIDs('Subsite');
$mockWrites = array(
'35910:File:a:0:{}' => array(
'base' => 'File',
'class' => 'File',
'id' => 35910,
'statefulids' => array(
array(
'id' => 35910,
'state' => array(),
),
),
'fields' => array(
'File:ClassName' => 'Image',
'File:ShowInSearch' => 1,
'File:ParentID' => 26470,
'File:Filename' => 'assets/Uploads/pic.jpg',
'File:Name' => 'pic.jpg',
'File:Title' => 'pic',
'File:SubsiteID' => 0,
'File:OwnerID' => 661,
'File:CurrentVersionID' => 22038,
'File:LastEdited' => '2016-12-09 00:35:13',
),
),
);
$variant = new SearchVariantSubsites();
$tmpMockWrites = $mockWrites;
$variant->extractManipulationWriteState($tmpMockWrites);
foreach($tmpMockWrites as $mockWrite) {
$this->assertCount(count($subsiteIDs), $mockWrite['statefulids']);
foreach ($mockWrite['statefulids'] as $statefulIDs) {
$this->assertTrue(
in_array($statefulIDs['state']['SearchVariantSubsites'], $subsiteIDs),
sprintf('Failed to assert that %s is in list of valid subsites: %s', $statefulIDs['state']['SearchVariantSubsites'], implode(', ', $subsiteIDs))
);
}
}

$subsite = $this->objFromFixture('Subsite', 'subsite1');
$tmpMockWrites = $mockWrites;
$tmpMockWrites['35910:File:a:0:{}']['fields']['File:SubsiteID'] = $subsite->ID;

$variant->extractManipulationWriteState($tmpMockWrites);
foreach($tmpMockWrites as $mockWrite) {
$this->assertCount(1, $mockWrite['statefulids']);
$statefulIDs = array_shift($mockWrite['statefulids']);
$this->assertEquals($subsite->ID, $statefulIDs['state']['SearchVariantSubsites']);
}
}

}

class SolrIndexSubsitesTest_Index extends SolrIndex
Expand Down

0 comments on commit b17bee7

Please sign in to comment.