Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
128 changes: 128 additions & 0 deletions _build/test/Tests/Processors/ProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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;

use MODX\Revolution\MODxTestCase;
use MODX\Revolution\Processors\Processor;

/**
* Tests for Processor::getBooleanProperty() normalization.
*
* @package modx-test
* @subpackage modx
* @group Processors
* @group Processor
*/
class ProcessorTest extends MODxTestCase
{
/**
* @return Processor
*/
private function createProcessor(array $properties = []): Processor
{
return new class ($this->modx, $properties) extends Processor {
public function process()
{
return $this->success();
}
};
}

public function testGetBooleanPropertyStringTrue(): void
{
$p = $this->createProcessor(['flag' => 'true']);
$this->assertTrue($p->getBooleanProperty('flag', false));
}

public function testGetBooleanPropertyStringTrueCaseInsensitive(): void
{
$p = $this->createProcessor(['flag' => 'TRUE']);
$this->assertTrue($p->getBooleanProperty('flag', false));

$p = $this->createProcessor(['flag' => 'True']);
$this->assertTrue($p->getBooleanProperty('flag', false));
}

public function testGetBooleanPropertyStringTrueTrimmed(): void
{
$p = $this->createProcessor(['flag' => ' true ']);
$this->assertTrue($p->getBooleanProperty('flag', false));
}

public function testGetBooleanPropertyStringFalse(): void
{
$p = $this->createProcessor(['flag' => 'false']);
$this->assertFalse($p->getBooleanProperty('flag', true));
}

public function testGetBooleanPropertyStringFalseCaseInsensitive(): void
{
$p = $this->createProcessor(['flag' => 'FALSE']);
$this->assertFalse($p->getBooleanProperty('flag', true));

$p = $this->createProcessor(['flag' => ' False ']);
$this->assertFalse($p->getBooleanProperty('flag', true));
}

public function testGetBooleanPropertyOnYesOne(): void
{
$p = $this->createProcessor(['a' => 'on', 'b' => 'yes', 'c' => '1']);
$this->assertTrue($p->getBooleanProperty('a', false));
$this->assertTrue($p->getBooleanProperty('b', false));
$this->assertTrue($p->getBooleanProperty('c', false));
}

public function testGetBooleanPropertyOffNoZero(): void
{
$p = $this->createProcessor(['a' => 'off', 'b' => 'no', 'c' => '0']);
$this->assertFalse($p->getBooleanProperty('a', true));
$this->assertFalse($p->getBooleanProperty('b', true));
$this->assertFalse($p->getBooleanProperty('c', true));
}

public function testGetBooleanPropertyMissingKeyReturnsDefault(): void
{
$p = $this->createProcessor([]);
$this->assertFalse($p->getBooleanProperty('missing', false));
$this->assertTrue($p->getBooleanProperty('missing', true));
}

public function testGetBooleanPropertyNativeBoolean(): void
{
$p = $this->createProcessor(['t' => true, 'f' => false]);
$this->assertTrue($p->getBooleanProperty('t', false));
$this->assertFalse($p->getBooleanProperty('f', true));
}

public function testGetBooleanPropertyNonStringCastedToBool(): void
{
$p = $this->createProcessor(['one' => 1, 'zero' => 0]);
$this->assertTrue($p->getBooleanProperty('one', false));
$this->assertFalse($p->getBooleanProperty('zero', true));
}

public function testGetBooleanPropertyUnknownStringReturnsDefault(): void
{
$p = $this->createProcessor(['key' => 'unknown']);
$this->assertFalse($p->getBooleanProperty('key', false));
$this->assertTrue($p->getBooleanProperty('key', true));
}

public function testGetBooleanPropertyEmptyStringReturnsDefault(): void
{
$p = $this->createProcessor(['key' => '']);
$this->assertFalse($p->getBooleanProperty('key', false));
$this->assertTrue($p->getBooleanProperty('key', true));
}
}
1 change: 1 addition & 0 deletions _build/test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<directory>Tests/Controllers/Context/</directory>
</testsuite>
<testsuite name="Processors">
<file>Tests/Processors/ProcessorTest.php</file>
<directory>Tests/Processors/Browser</directory>
<directory>Tests/Processors/Context</directory>
<directory>Tests/Processors/Element</directory>
Expand Down
26 changes: 26 additions & 0 deletions core/src/Revolution/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function run()

/**
* Get a specific property.
*
* @param string $k
* @param mixed $default
* @return mixed
Expand All @@ -223,6 +224,31 @@ public function getProperty($k, $default = null)
return array_key_exists($k, $this->properties) ? $this->properties[$k] : $default;
}

/**
* Get a property as boolean. Normalizes string "true"/"false" (case-insensitive, trimmed)
* and "on"/"1"/"yes" to true, "off"/"0"/"no" to false. Use when JS sends boolean params as strings.
*
* @param string $k
* @param bool $default
* @return bool
*/
public function getBooleanProperty($k, $default = false): bool
{
$value = $this->getProperty($k, $default);
if (is_bool($value)) {
return $value;
}
if (!is_string($value)) {
return (bool) $value;
}
$v = strtolower(trim($value));
return match ($v) {
'true', '1', 'on', 'yes' => true,
'false', '0', 'off', 'no' => false,
default => $default,
};
}

/**
* Set a property value
*
Expand Down
6 changes: 3 additions & 3 deletions core/src/Revolution/Processors/Resource/Get.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -30,8 +31,7 @@ public function process()
{
$resourceArray = $this->object->toArray();
$resourceArray['canpublish'] = $this->modx->hasPermission('publish_document');
if (!$this->getProperty('skipFormatDates') ||
($this->getProperty('skipFormatDates') && $this->getProperty('skipFormatDates') == 'false')) {
if (!$this->getBooleanProperty('skipFormatDates', false)) {
Comment thread
Ibochkarev marked this conversation as resolved.
Outdated
$this->formatDates($resourceArray);
}
return $this->success('', $resourceArray);
Expand All @@ -51,7 +51,7 @@ public function formatDates(array &$resourceArray)
} else {
$resourceArray['unpub_date'] = '';
}
if (!empty($resourceArray) && $resourceArray['publishedon'] != '0000-00-00 00:00:00') {
if (!empty($resourceArray['publishedon']) && $resourceArray['publishedon'] != '0000-00-00 00:00:00') {
$resourceArray['publishedon'] = date($format, strtotime($resourceArray['publishedon']));
} else {
$resourceArray['publishedon'] = '';
Expand Down