From 639480d433af5d93c2bbc4b958d060f816d7b5ce Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 2 Nov 2016 11:54:13 +1300 Subject: [PATCH] Add ability to force a return value for "is_frontend" Resolves #236 --- code/Fluent.php | 22 ++++++++++++++++++ tests/FluentTest.php | 53 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/code/Fluent.php b/code/Fluent.php index ee7c7e84..9e41cbe5 100644 --- a/code/Fluent.php +++ b/code/Fluent.php @@ -8,6 +8,13 @@ */ class Fluent extends Object implements TemplateGlobalProvider { + /** + * If set, "is_frontend" can be forced to return a value + * + * @var boolean + */ + protected static $force_is_frontend; + /** * Forces regeneration of all locale routes */ @@ -404,6 +411,10 @@ public static function default_locale($domain = null) */ public static function is_frontend($ignoreController = false) { + // Option to force a frontend response if required + if (null !== self::$force_is_frontend) { + return (bool) self::$force_is_frontend; + } // No controller - Possibly pre-route phase, so check URL if ($ignoreController || !Controller::has_curr()) { @@ -739,4 +750,15 @@ public static function isFieldModified(DataObject $object, FormField $field, $lo return true; } + + /** + * Sets whether to force "is_frontend" to return true or false. This can be used for situations where the + * frontend controllers are not involved, e.g. API modules, unit testing or retrieving data programmatically. + * + * @param boolean $force + */ + public static function set_force_is_frontend($force = true) + { + self::$force_is_frontend = $force; + } } diff --git a/tests/FluentTest.php b/tests/FluentTest.php index db5fbf8e..357fea3d 100644 --- a/tests/FluentTest.php +++ b/tests/FluentTest.php @@ -470,7 +470,7 @@ public function testFilteredObjects() Fluent::set_persist_locale('fr_CA'); } - /* + /** * Test that locale filter can be augmented properly */ public function testUpdateFilteredObject() @@ -496,6 +496,57 @@ public function testUpdateFilteredObject() $this->assertEquals(array(), $ids); } + /** + * Tests that the "is_frontend" method can be forced to return true or false. This affects locale filtering. + * + * @covers Fluent::set_force_is_frontend + * @dataProvider forceFrontendProvider + */ + public function testForceIsFrontend($locale, $force, $expected) + { + Fluent::set_persist_locale($locale); + // May the force be with you + Fluent::set_force_is_frontend($force); + + $result = DataObject::get('FluentTest_FilteredObject') + ->sort('Title') + ->column('Title'); + + $this->assertSame($expected, $result); + + // Reset to prevent messing up other tests + Fluent::set_force_is_frontend(null); + } + + /** + * @return array + */ + public function forceFrontendProvider() + { + return array( + array( + 'en_NZ', + false, + array('filtered 1', 'filtered 2', 'filtered 3') + ), + array( + 'en_NZ', + true, + array('filtered 1', 'filtered 2') + ), + array( + 'es_ES', + false, + array('filtered 1', 'filtered 2', 'filtered 3') + ), + array( + 'es_ES', + true, + array('filtered 2') + ) + ); + } + /** * Test auto-scaffolding of CMS fields */