Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions _build/test/Tests/Model/modXSwitchContextCultureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
* Copyright (c) MODX, LLC
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*
* @package modx-test
*/

namespace MODX\Revolution\Tests\Model;

use MODX\Revolution\modContextSetting;
use MODX\Revolution\MODxTestCase;

/**
* Regression for culture re-init inside switchContext (#14962).
*
* @group Model
* @group modX
*/
class modXSwitchContextCultureTest extends MODxTestCase
{
public function testSwitchContextReturnsFalseWhenContextMissing()
{
$previous = $this->modx->context;
$this->modx->context = null;
try {
$this->assertFalse($this->modx->switchContext('web'));
} finally {
$this->modx->context = $previous;
}
}

public function testSwitchContextReinitializesCultureKey()
{
$originalKey = $this->modx->context->get('key');
$targetKey = ($originalKey === 'web') ? 'mgr' : 'web';

$setting = $this->modx->getObject(modContextSetting::class, [
'context_key' => $targetKey,
'key' => 'cultureKey',
]);
$created = false;
if (!$setting) {
$setting = $this->modx->newObject(modContextSetting::class);
$setting->fromArray([
'context_key' => $targetKey,
'key' => 'cultureKey',
'value' => 'de',
'xtype' => 'textfield',
'namespace' => 'core',
'area' => 'language',
], '', true);
$this->assertTrue((bool)$setting->save());
$created = true;
}
$previousValue = $setting->get('value');
$setting->set('value', 'de');
$setting->save();

// Avoid session/request overrides masking the context cultureKey.
unset($_SESSION['cultureKey'], $_REQUEST['cultureKey']);

try {
$switched = $this->modx->switchContext($targetKey, true);
$this->assertTrue($switched, 'Expected switchContext to succeed');
$this->assertSame('de', $this->modx->cultureKey);
$this->assertSame('de', $this->modx->getOption('cultureKey'));
} finally {
$this->modx->switchContext($originalKey, true);
if ($created) {
$setting->remove();
} else {
$setting->set('value', $previousValue);
$setting->save();
}
}
}
}
1 change: 1 addition & 0 deletions _build/test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</testsuite>
<testsuite name="Model">
<file>Tests/Model/modXTest.php</file>
<file>Tests/Model/modXSwitchContextCultureTest.php</file>
<file>Tests/Model/modXLoggingTest.php</file>
<file>Tests/Model/modParserTest.php</file>
<directory>Tests/Model/Dashboard</directory>
Expand Down
17 changes: 14 additions & 3 deletions core/src/Revolution/modX.php
Original file line number Diff line number Diff line change
Expand Up @@ -2133,14 +2133,25 @@ public function addEventListener($event, $pluginId, $propertySetName = '') {
* before being switched to.
* @return boolean True if the switch was successful, otherwise false.
*/
public function switchContext($contextKey, $reload = false) {
$switched= false;
public function switchContext($contextKey, $reload = false)
{
$switched = false;
if ($this->context === null) {
return $switched;
}
if ($this->context->key != $contextKey) {
$switched= $this->_initContext($contextKey, $reload);
$switched = $this->_initContext($contextKey, $reload);
if ($switched) {
if (is_array($this->config)) {
$this->setPlaceholders($this->config, '+');
}
/*
* Reconcile session/culture for the new context options without
* exposing public initSession/initCulture (see #14962 / opengeek).
* _initSession is a no-op when a session is already active.
*/
$this->_initSession(null);
$this->_initCulture(null);
}
}
return $switched;
Expand Down
Loading