-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from moreonion/layout-context
[layout] add context conditions
- Loading branch information
Showing
7 changed files
with
209 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Drupal\campaignion_layout\Context; | ||
|
||
use Drupal\little_helpers\Services\Container; | ||
use Drupal\campaignion_layout\Lookup; | ||
|
||
/** | ||
* Expose available layouts as a context condition. | ||
*/ | ||
class LayoutCondition extends \context_condition { | ||
|
||
/** | ||
* Condition values. | ||
*/ | ||
public function condition_values() { | ||
return Container::get()->loadService('campaignion_layout.themes')->layoutOptions(); | ||
} | ||
|
||
/** | ||
* Check whether the condition is met. | ||
* | ||
* @param string $active_layout_name | ||
* Machine name of the active layout if one was set. | ||
*/ | ||
public function execute(string $active_layout_name) { | ||
foreach ($this->get_contexts($active_layout_name) as $context) { | ||
$this->condition_met($context, $active_layout_name); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Drupal\campaignion_layout\Context; | ||
|
||
use Drupal\campaignion_layout\Tests\ThemesBaseTest; | ||
|
||
/** | ||
* Test for the layout context condition implementation. | ||
*/ | ||
class LayoutConditionTest extends ThemesBaseTest { | ||
|
||
/** | ||
* Test the available form options for the context condition plugin. | ||
*/ | ||
public function testFormOptions() { | ||
$themes['foo']['title'] = 'Foo'; | ||
$themes['foo']['layouts']['1col']['title'] = 'Single column'; | ||
$themes['bar']['title'] = 'Bar'; | ||
$themes['bar']['layouts']['2col']['title'] = 'Two columns'; | ||
$this->injectThemes($themes); | ||
$condition = new LayoutCondition('plugin', []); | ||
$this->assertEqual([ | ||
'1col' => 'Single column', | ||
'2col' => 'Two columns', | ||
], $condition->condition_values()); | ||
} | ||
|
||
/** | ||
* Test the behaviour of the execute function. | ||
*/ | ||
public function testExecuteWithMultipleContexts() { | ||
$mock_condition = $this->getMockBuilder(LayoutCondition::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['condition_met', 'get_contexts']) | ||
->getMock(); | ||
$active_layout = 'active_layout'; | ||
$matching_contexts = ['one', 'two', 'three']; | ||
$mock_condition->expects($this->once()) | ||
->method('get_contexts') | ||
->with($active_layout) | ||
->willReturn($matching_contexts); | ||
$mock_condition->expects($this->exactly(3)) | ||
->method('condition_met') | ||
->withConsecutive( | ||
['one', $active_layout], | ||
['two', $active_layout], | ||
['three', $active_layout] | ||
); | ||
$mock_condition->execute($active_layout); | ||
} | ||
|
||
/** | ||
* Test the behaviour of the execute function. | ||
*/ | ||
public function testExecuteWithNoContexts() { | ||
$mock_condition = $this->getMockBuilder(LayoutCondition::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['condition_met', 'get_contexts']) | ||
->getMock(); | ||
$theme = 'theme_name'; | ||
$matching_contexts = []; | ||
$mock_condition->expects($this->once()) | ||
->method('get_contexts') | ||
->with('theme_name') | ||
->willReturn($matching_contexts); | ||
$mock_condition->expects($this->exactly(0)) | ||
->method('condition_met'); | ||
$mock_condition->execute($theme); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Drupal\campaignion_layout\Tests; | ||
|
||
use Drupal\little_helpers\Services\Container; | ||
use Upal\DrupalUnitTestCase; | ||
|
||
use Drupal\campaignion_layout\Theme; | ||
use Drupal\campaignion_layout\Themes; | ||
|
||
/** | ||
* Base test for testing theme & layout handling. | ||
* | ||
* The test class provides a simple way of injecting a Themes service with | ||
* themes and layouts configured. | ||
*/ | ||
abstract class ThemesBaseTest extends DrupalUnitTestCase { | ||
|
||
/** | ||
* Cleanup the injected service. | ||
*/ | ||
public function tearDown() : void { | ||
Container::get()->inject('campaignion_layout.themes', NULL); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* Inject a themes service with specific theme and layout data. | ||
*/ | ||
protected function injectThemes(array $themes = [], array $layouts = []) { | ||
$theme_objects = []; | ||
$add_layout_defaults = function ($info) { | ||
return $info + ['fields' => []]; | ||
}; | ||
foreach ($themes as $name => $data) { | ||
$data += ['layouts' => []]; | ||
$theme = $this->getMockBuilder(Theme::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['title', 'layouts']) | ||
->getMock(); | ||
$theme->method('title')->willReturn($data['title'] ?? $name); | ||
$theme->method('layouts') | ||
->willReturn(array_map($add_layout_defaults, $data['layouts'])); | ||
$theme_objects[$name] = $theme; | ||
$layouts += $data['layouts']; | ||
} | ||
$themes = $this->getMockBuilder(Themes::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['enabledThemes', 'declaredLayouts']) | ||
->getMock(); | ||
$themes->method('enabledThemes')->willReturn($theme_objects); | ||
$themes->method('declaredLayouts') | ||
->willReturn(array_map($add_layout_defaults, $layouts)); | ||
Container::get()->inject('campaignion_layout.themes', $themes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters