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
72 changes: 72 additions & 0 deletions _build/test/Tests/Model/modParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1060,9 +1060,81 @@ public function providerParsePropertyString() {
" &property=`value with nested backticks```",
true
],
[
['prop' => 'value', 'prop2' => 100],
'{"prop":"value","prop2":100}',
true
],
[
[
'prop' => [
'name' => 'prop',
'desc' => '',
'type' => 'textfield',
'options' => [],
'value' => 'value'
],
'prop2' => [
'name' => 'prop2',
'desc' => '',
'type' => 'textfield',
'options' => [],
'value' => 100
],
],
'{"prop":"value","prop2":100}',
false
],
];
}

/**
* Test modParser->parseProperties() with JSON string.
*/
public function testParsePropertiesJson()
{
$parser = $this->modx->parser;
$props = $parser->parseProperties('{"a":1,"b":"two"}');
$this->assertIsArray($props);
$this->assertSame(1, $props['a']);
$this->assertSame('two', $props['b']);
}

/**
* Test [[@if]]...[[@else]]...[[@endif]] conditional blocks.
*/
public function testConditionalBlocks()
{
$content = '[[@if ($modx->user->id == 1)]]yes[[@else]]no[[@endif]]';
$this->modx->parser->processElementTags('', $content, true, false, '[[', ']]', [], 1);
$userId = $this->modx->user ? (int) $this->modx->user->get('id') : 0;
$this->assertSame($userId === 1 ? 'yes' : 'no', trim($content));
}

/**
* Test [[@if]] without [[@else]] - empty when false.
*/
public function testConditionalBlocksNoElse()
{
$content = '[[@if ($modx->user->id == 99999)]]show[[@endif]]';
$this->modx->parser->processElementTags('', $content, true, false, '[[', ']]', [], 1);
$this->assertSame('', trim($content));
}

/**
* Test [[~contextKey]] link tag (e.g. [[~web]]) resolves to context site_start URL or empty.
*/
public function testLinkTagContextKey()
{
$content = '[[~web]]';
$this->modx->parser->processElementTags('', $content, true, false, '[[', ']]', [], 1);
$this->assertIsString($content);
if ($content !== '' && $content !== '[[~web]]') {
$hasPathOrQuery = strpos($content, '/') !== false || strpos($content, '?') !== false;
$this->assertTrue($hasPathOrQuery, 'Context link should produce a URL (path or query string)');
}
}

/**
* Test modParser->realname().
*
Expand Down
53 changes: 43 additions & 10 deletions core/src/Revolution/modLinkTag.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
Expand All @@ -14,18 +15,19 @@
* Represents link tags.
*
* [[~12]] Creates a URL from the specified resource identifier.
* [[~web]] Creates a URL to the site_start resource of the context (e.g. web).
*
* @package MODX\Revolution
*/
class modLinkTag extends modTag
{
/**
* Overrides modTag::__construct to set the Link Tag token
* Overrides modTag::__construct to set the Link Tag token.
* {@inheritdoc}
*/
function __constructor(modX & $modx)
public function __construct(modX &$modx)
{
parent:: __construct($modx);
parent::__construct($modx);
$this->setToken('~');
}

Expand All @@ -36,11 +38,10 @@ function __constructor(modX & $modx)
*/
public function process($properties = null, $content = null)
{
parent:: process($properties, $content);
parent::process($properties, $content);
if (!$this->_processed) {
$this->_output = $this->_content;
if (is_string($this->_output) && !empty ($this->_output)) {
/* collect element tags in the content and process them */
if (is_string($this->_output) && !empty($this->_output)) {
$maxIterations = intval($this->modx->getOption('parser_max_iterations', null, 10));
$this->modx->parser->processElementTags(
$this->_tag,
Expand All @@ -52,8 +53,8 @@ public function process($properties = null, $content = null)
[],
$maxIterations
);
$context = '';
if ($this->modx->getOption('friendly_urls', null, false)) {
$context = $this->resolveContextLinkTarget();
if ($this->modx->getOption('friendly_urls', null, false) && $context === '' && !empty($this->_output)) {
if (array_key_exists('context', $this->_properties)) {
$context = $this->_properties['context'];
}
Expand Down Expand Up @@ -116,8 +117,40 @@ public function process($properties = null, $content = null)
}
}

/* finally, return the processed element content */

return $this->_output;
}

/**
* Resolves [[~contextKey]] to site_start resource id and returns the context key.
* If link target is numeric or empty, leaves _output unchanged and returns ''.
*
* @return string Context key or empty string
*/
private function resolveContextLinkTarget()
{
$linkTarget = $this->_output;
if (is_numeric($linkTarget) || $linkTarget === '') {
return '';
}
$ctx = $this->modx->getContext($linkTarget);
if (!$ctx instanceof modContext) {
$this->modx->log(
modX::LOG_LEVEL_WARN,
'Link tag unknown context: `' . $this->_tag . '`'
);
$this->_output = '';
return '';
}
$siteStart = $ctx->getOption('site_start');
if ($siteStart !== null && $siteStart !== '') {
$this->_output = (string) $siteStart;
return $linkTarget;
}
$this->modx->log(
modX::LOG_LEVEL_WARN,
'Link tag context has no site_start: `' . $this->_tag . '`'
);
$this->_output = '';
return '';
}
}
Loading