Skip to content

Commit

Permalink
Merge pull request #237 from robbieaverill/feature/force-frontend
Browse files Browse the repository at this point in the history
Add ability to force a return value for "is_frontend"
  • Loading branch information
Damian Mooyman authored Nov 10, 2016
2 parents 66143da + 639480d commit ab23171
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
22 changes: 22 additions & 0 deletions code/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
}
53 changes: 52 additions & 1 deletion tests/FluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public function testFilteredObjects()
Fluent::set_persist_locale('fr_CA');
}

/*
/**
* Test that locale filter can be augmented properly
*/
public function testUpdateFilteredObject()
Expand All @@ -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
*/
Expand Down

0 comments on commit ab23171

Please sign in to comment.