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
156 changes: 156 additions & 0 deletions _build/test/Tests/Processors/Element/StaticFileChangedUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?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\Processors\Element;

use MODX\Revolution\modChunk;
use MODX\Revolution\modPlugin;
use MODX\Revolution\modSnippet;
use MODX\Revolution\modTemplate;
use MODX\Revolution\modTemplateVar;
use MODX\Revolution\MODxTestCase;
use MODX\Revolution\Processors\Element\Chunk\Update as ChunkUpdate;
use MODX\Revolution\Processors\Element\Plugin\Update as PluginUpdate;
use MODX\Revolution\Processors\Element\Snippet\Update as SnippetUpdate;
use MODX\Revolution\Processors\Element\Template\Update as TemplateUpdate;
use MODX\Revolution\Processors\Element\TemplateVar\Update as TemplateVarUpdate;

/**
* Ensures static_file_changed is returned for every element Update processor (#13235).
*
* @group Processors
* @group Element
*/
class StaticFileChangedUpdateTest extends MODxTestCase
{
/** @var string */
private $tmpDir;

/**
* @before
*/
public function setUpFixtures()
{
parent::setUpFixtures();
$this->tmpDir = sys_get_temp_dir() . '/modx-static-file-changed-' . uniqid('', true);
mkdir($this->tmpDir, 0777, true);
}

/**
* @after
*/
public function tearDownFixtures()
{
parent::tearDownFixtures();
foreach (
[
modChunk::class,
modSnippet::class,
modPlugin::class,
modTemplate::class,
modTemplateVar::class,
] as $class
) {
$objects = $this->modx->getCollection($class, [
'name:LIKE' => 'UnitTestStaticFile%',
]);
if ($class === modTemplate::class) {
$objects = $this->modx->getCollection($class, [
'templatename:LIKE' => 'UnitTestStaticFile%',
]);
}
foreach ($objects as $object) {
$object->remove();
}
}
if ($this->tmpDir && is_dir($this->tmpDir)) {
foreach (glob($this->tmpDir . '/*') ?: [] as $file) {
@unlink($file);
}
@rmdir($this->tmpDir);
}
$this->modx->error->reset();
}

/**
* @dataProvider providerElementTypes
*/
public function testStaticFileChangedFlagOnPathChange(
string $classKey,
string $processor,
string $nameField,
string $contentField,
string $contentValue
) {
$fileA = $this->tmpDir . '/a.' . substr(md5($classKey), 0, 6) . '.txt';
$fileB = $this->tmpDir . '/b.' . substr(md5($classKey), 0, 6) . '.txt';
file_put_contents($fileA, $contentValue);
file_put_contents($fileB, $contentValue);

/** @var \MODX\Revolution\modElement $element */
$element = $this->modx->newObject($classKey);
$element->fromArray([
$nameField => 'UnitTestStaticFile' . substr(md5($classKey), 0, 6),
'description' => 'static file changed test',
'static' => true,
'static_file' => $fileA,
$contentField => $contentValue,
]);
$this->assertTrue((bool)$element->save(), 'Failed to create static ' . $classKey);

$payload = [
'id' => $element->get('id'),
$nameField => $element->get($nameField),
'description' => $element->get('description'),
'category' => 0,
'locked' => false,
'static' => 1,
'static_file' => $fileB,
$contentField => $contentValue,
'clearCache' => false,
];
if ($classKey === modPlugin::class) {
$payload['disabled'] = false;
}

$result = $this->modx->runProcessor($processor, $payload);
$this->assertTrue($this->checkForSuccess($result), $result->getMessage());
$object = $result->getObject();
$this->assertArrayHasKey('static_file_changed', $object);
$this->assertTrue(
(bool)$object['static_file_changed'],
'Expected static_file_changed=true after path change for ' . $classKey
);

$payload['static_file'] = $fileB;
$result = $this->modx->runProcessor($processor, $payload);
$this->assertTrue($this->checkForSuccess($result), $result->getMessage());
$object = $result->getObject();
$this->assertArrayHasKey('static_file_changed', $object);
$this->assertFalse(
(bool)$object['static_file_changed'],
'Expected static_file_changed=false when path unchanged for ' . $classKey
);
}

public function providerElementTypes(): array
{
return [
'chunk' => [modChunk::class, ChunkUpdate::class, 'name', 'snippet', '<p>static</p>'],
'snippet' => [modSnippet::class, SnippetUpdate::class, 'name', 'snippet', 'return "static";'],
'plugin' => [modPlugin::class, PluginUpdate::class, 'name', 'plugincode', 'return true;'],
'template' => [modTemplate::class, TemplateUpdate::class, 'templatename', 'content', '<html>static</html>'],
'tv' => [modTemplateVar::class, TemplateVarUpdate::class, 'name', 'default_text', 'static'],
];
}
}
4 changes: 1 addition & 3 deletions core/src/Revolution/Processors/Element/Chunk/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public function beforeSave()

public function cleanup()
{
return $this->success('',
array_merge($this->object->get(['id', 'name', 'description', 'locked', 'category', 'snippet']),
['previous_category' => $this->previousCategory]));
return $this->success('', $this->getCleanupData(['id', 'name', 'description', 'locked', 'category', 'snippet']));
}
}
7 changes: 3 additions & 4 deletions core/src/Revolution/Processors/Element/Plugin/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ public function setSystemEvents()

public function cleanup()
{
return $this->success('', array_merge(
$this->object->get(['id', 'name', 'description', 'locked', 'category', 'disabled', 'plugincode']),
['previous_category' => $this->previousCategory]
));
return $this->success('', $this->getCleanupData([
'id', 'name', 'description', 'locked', 'category', 'disabled', 'plugincode',
]));
}
}
4 changes: 1 addition & 3 deletions core/src/Revolution/Processors/Element/Snippet/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public function beforeSave()

public function cleanup()
{
return $this->success('',
array_merge($this->object->get(['id', 'name', 'description', 'locked', 'category', 'snippet']),
['previous_category' => $this->previousCategory]));
return $this->success('', $this->getCleanupData(['id', 'name', 'description', 'locked', 'category', 'snippet']));
}
}
6 changes: 3 additions & 3 deletions core/src/Revolution/Processors/Element/TemplateVar/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ public function setMediaSources()

public function cleanup()
{
return $this->success('',
array_merge($this->object->get(['id', 'name', 'description', 'locked', 'category', 'default_text']),
['previous_category' => $this->previousCategory]));
return $this->success('', $this->getCleanupData([
'id', 'name', 'description', 'locked', 'category', 'default_text',
]));
}
}

30 changes: 24 additions & 6 deletions core/src/Revolution/Processors/Element/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class Update extends UpdateProcessor
public $previousCategory;
/** @var modElement $object */
public $object;
/** @var bool Whether static source identity changed (path/source/static); triggers manager reload */
protected bool $staticFileChanged = false;

public function beforeSet()
{
Expand Down Expand Up @@ -79,6 +81,9 @@ public function beforeSave()
}
}

/* Reload manager form when static source identity changes (path, media source, or static flag). */
$this->staticFileChanged = $this->object->staticSourceChanged();

return !$this->hasErrors();
}

Expand All @@ -99,13 +104,26 @@ public function afterSave()
}
}

/**
* Build success payload shared by all element Update processors.
* Subclasses must use this so static_file_changed is never dropped.
*
* @param array $fields Object fields to include
* @return array
*/
protected function getCleanupData(array $fields): array
{
return array_merge($this->object->get($fields), [
'previous_category' => $this->previousCategory,
'static_file_changed' => $this->staticFileChanged,
]);
}

public function cleanup()
{
$fields = array('id', 'description', 'locked', 'category', 'content');
array_push($fields, ($this->classKey == modTemplate::class ? 'templatename' : 'name'));
return $this->success(
'',
array_merge($this->object->get($fields), ['previous_category' => $this->previousCategory])
);
$fields = ['id', 'description', 'locked', 'category', 'content'];
$fields[] = ($this->classKey == modTemplate::class ? 'templatename' : 'name');

return $this->success('', $this->getCleanupData($fields));
}
}
13 changes: 13 additions & 0 deletions manager/assets/modext/core/modx.layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,16 @@ MODx.loadPage = MODx.LayoutMgr.loadPage;
MODx.showDashboard = MODx.LayoutMgr.showDashboard;
MODx.hideDashboard = MODx.LayoutMgr.hideDashboard;
MODx.changeMenu = MODx.LayoutMgr.changeMenu;

/**
* If the element update response indicates static_file path changed, reload the page and return true.
* @param {Object} resultObject - response.result.object from element update processor
* @returns {boolean} true if page reload was triggered
*/
MODx.reloadIfStaticFileChanged = function(resultObject) {
if (resultObject && resultObject.static_file_changed) {
MODx.loadPage(MODx.request.a, `id=${resultObject.id}`);
return true;
}
return false;
};
6 changes: 4 additions & 2 deletions manager/assets/modext/widgets/element/modx.panel.chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,18 @@ Ext.extend(MODx.panel.Chunk, MODx.FormPanel, {
},

success: function(r) {
const data = r.result.object;
if (MODx.reloadIfStaticFileChanged(data)) { return; }
if (MODx.request.id) { Ext.getCmp('modx-grid-element-properties').save(); }
this.getForm().setValues(r.result.object);
this.getForm().setValues(data);

const
c = Ext.getCmp('modx-chunk-category').getValue(),
n = c !== '' && c !== null && c !== 0 ? `n_chunk_category_${c}` : 'n_type_chunk',
t = Ext.getCmp('modx-tree-element')
;
if (t) {
const node = t.getNodeById(`n_chunk_element_${Ext.getCmp('modx-chunk-id').getValue()}_${r.result.object.previous_category}`);
const node = t.getNodeById(`n_chunk_element_${Ext.getCmp('modx-chunk-id').getValue()}_${data.previous_category}`);
if (node) { node.destroy(); }
t.refreshNode(n, true);
}
Expand Down
6 changes: 4 additions & 2 deletions manager/assets/modext/widgets/element/modx.panel.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,18 @@ Ext.extend(MODx.panel.Plugin, MODx.FormPanel, {
},

success: function(o) {
const data = o.result.object;
if (MODx.reloadIfStaticFileChanged(data)) { return; }
if (MODx.request.id) { Ext.getCmp('modx-grid-element-properties').save(); }
Ext.getCmp('modx-grid-plugin-event').getStore().commitChanges();
this.getForm().setValues(o.result.object);
this.getForm().setValues(data);

const t = Ext.getCmp('modx-tree-element');
if (t) {
const
c = Ext.getCmp('modx-plugin-category').getValue(),
u = c !== '' && c != null && c !== 0 ? `n_plugin_category_${c}` : 'n_type_plugin',
node = t.getNodeById(`n_plugin_element_${Ext.getCmp('modx-plugin-id').getValue()}_${o.result.object.previous_category}`)
node = t.getNodeById(`n_plugin_element_${Ext.getCmp('modx-plugin-id').getValue()}_${data.previous_category}`)
;
if (node) { node.destroy(); }
t.refreshNode(u, true);
Expand Down
6 changes: 4 additions & 2 deletions manager/assets/modext/widgets/element/modx.panel.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,19 @@ Ext.extend(MODx.panel.Snippet, MODx.FormPanel, {
},

success: function(r) {
const data = r.result.object;
if (MODx.reloadIfStaticFileChanged(data)) { return; }
if (MODx.request.id) {
Ext.getCmp('modx-grid-element-properties').save();
}
this.getForm().setValues(r.result.object);
this.getForm().setValues(data);

const t = Ext.getCmp('modx-tree-element');
if (t) {
const
c = Ext.getCmp('modx-snippet-category').getValue(),
u = c !== '' && c != null && c !== 0 ? `n_snippet_category_${c}` : 'n_type_snippet',
node = t.getNodeById(`n_snippet_element_${Ext.getCmp('modx-snippet-id').getValue()}_${r.result.object.previous_category}`)
node = t.getNodeById(`n_snippet_element_${Ext.getCmp('modx-snippet-id').getValue()}_${data.previous_category}`)
;
if (node) {
node.destroy();
Expand Down
7 changes: 3 additions & 4 deletions manager/assets/modext/widgets/element/modx.panel.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,9 @@ Ext.extend(MODx.panel.Template, MODx.FormPanel, {
});
},
success: function(response) {
const
data = response.result.object,
tree = Ext.getCmp('modx-tree-element')
;
const data = response.result.object;
if (MODx.reloadIfStaticFileChanged(data)) { return; }
const tree = Ext.getCmp('modx-tree-element');
if (MODx.request.id) {
Ext.getCmp('modx-grid-element-properties').save();
}
Expand Down
6 changes: 4 additions & 2 deletions manager/assets/modext/widgets/element/modx.panel.tv.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,18 +752,20 @@ Ext.extend(MODx.panel.TV, MODx.FormPanel, {
},

success: function(r) {
const data = r.result.object;
if (MODx.reloadIfStaticFileChanged(data)) { return; }
Ext.getCmp('modx-grid-tv-template').getStore().commitChanges();
Ext.getCmp('modx-grid-tv-security').getStore().commitChanges();
Ext.getCmp('modx-grid-element-sources').getStore().commitChanges();
if (MODx.request.id) { Ext.getCmp('modx-grid-element-properties').save(); }
this.getForm().setValues(r.result.object);
this.getForm().setValues(data);

const t = Ext.getCmp('modx-tree-element');
if (t) {
const
c = Ext.getCmp('modx-tv-category').getValue(),
u = c !== '' && c != null && c !== 0 ? `n_tv_category_${c}` : 'n_type_tv',
node = t.getNodeById(`n_tv_element_${Ext.getCmp('modx-tv-id').getValue()}_${r.result.object.previous_category}`);
node = t.getNodeById(`n_tv_element_${Ext.getCmp('modx-tv-id').getValue()}_${data.previous_category}`);
if (node) { node.destroy(); }
t.refreshNode(u, true);
}
Expand Down
Loading