2017-04-28 00:22:15 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
namespace SilverStripe\UserForms\Tests\Model;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
2024-09-10 01:28:45 +02:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
/**
|
|
|
|
* Class EditableCustomRulesTest
|
|
|
|
*/
|
|
|
|
class EditableCustomRuleTest extends SapphireTest
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
protected static $fixture_file = 'EditableCustomRuleTest.yml';
|
2017-04-28 00:22:15 +02:00
|
|
|
|
|
|
|
public function testBuildExpression()
|
|
|
|
{
|
|
|
|
/** @var EditableCustomRule $rule1 */
|
2017-08-09 01:55:09 +02:00
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
|
2017-04-28 00:22:15 +02:00
|
|
|
$result1 = $rule1->buildExpression();
|
|
|
|
|
|
|
|
//Dropdowns expect change event
|
|
|
|
$this->assertEquals('change', $result1['event']);
|
|
|
|
$this->assertNotEmpty($result1['operation']);
|
2017-08-11 02:20:12 +02:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
//Check for equals sign
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringContainsString('==', $result1['operation']);
|
2017-04-28 00:22:15 +02:00
|
|
|
|
|
|
|
/** @var EditableCustomRule $rule2 */
|
2017-08-09 01:55:09 +02:00
|
|
|
$rule2 = $this->objFromFixture(EditableCustomRule::class, 'rule2');
|
2017-04-28 00:22:15 +02:00
|
|
|
$result2 = $rule2->buildExpression();
|
2017-08-11 02:20:12 +02:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
//TextField expect change event
|
|
|
|
$this->assertEquals('keyup', $result2['event']);
|
|
|
|
$this->assertNotEmpty($result2['operation']);
|
2017-08-11 02:20:12 +02:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
//Check for greater than sign
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringContainsString('>', $result2['operation']);
|
2017-04-28 00:22:15 +02:00
|
|
|
}
|
2017-05-22 05:54:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that methods are returned for manipulating the presence of the "hide" CSS class depending
|
|
|
|
* on whether the field should be hidden or shown
|
|
|
|
*/
|
|
|
|
public function testToggleDisplayText()
|
|
|
|
{
|
2019-03-25 03:09:38 +01:00
|
|
|
/** @var EditableCustomRule $rule1 */
|
2017-08-09 01:55:09 +02:00
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
|
2017-05-22 05:54:28 +02:00
|
|
|
$this->assertSame('addClass("hide")', $rule1->toggleDisplayText('show'));
|
|
|
|
$this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('hide'));
|
2019-03-25 03:09:38 +01:00
|
|
|
$this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('show', true));
|
|
|
|
$this->assertSame('addClass("hide")', $rule1->toggleDisplayText('hide', true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testToggleDisplayEvent()
|
|
|
|
{
|
|
|
|
/** @var EditableCustomRule $rule1 */
|
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
|
|
|
|
$this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('show'));
|
|
|
|
$this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('hide'));
|
|
|
|
$this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('show', true));
|
|
|
|
$this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('hide', true));
|
2017-05-22 05:54:28 +02:00
|
|
|
}
|
2018-05-08 07:02:01 +02:00
|
|
|
|
2024-09-10 01:28:45 +02:00
|
|
|
public static function dataProviderValidateAgainstFormData()
|
2020-01-13 00:15:34 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'IsNotBlank with blank value' =>
|
|
|
|
['IsNotBlank', '', '', false],
|
|
|
|
'IsNotBlank with nopn-blank value' =>
|
|
|
|
['IsNotBlank', '', 'something', true],
|
|
|
|
'IsBlank with blank value' =>
|
|
|
|
['IsBlank', '', '', true],
|
|
|
|
'IsBlank with nopn-blank value' =>
|
|
|
|
['IsBlank', '', 'something', false],
|
|
|
|
'HasValue with blank value' =>
|
|
|
|
['HasValue', 'NZ', '', false],
|
|
|
|
'HasValue with correct value' =>
|
|
|
|
['HasValue', 'NZ', 'NZ', true],
|
|
|
|
'HasValue with incorrect value' =>
|
|
|
|
['HasValue', 'NZ', 'UK', false],
|
|
|
|
'ValueNot with blank value' =>
|
|
|
|
['ValueNot', 'NZ', '', true],
|
|
|
|
'ValueNot with targeted value' =>
|
|
|
|
['ValueNot', 'NZ', 'NZ', false],
|
|
|
|
'ValueNot with non-targeted value' =>
|
|
|
|
['ValueNot', 'NZ', 'UK', true],
|
|
|
|
'ValueLessThan with value below target' =>
|
|
|
|
['ValueLessThan', '0', '-0.00001', true],
|
|
|
|
'ValueLessThan with value equal to target' =>
|
|
|
|
['ValueLessThan', '0', '0', false],
|
|
|
|
'ValueLessThan with value greater to target' =>
|
|
|
|
['ValueLessThan', '0', '0.0001', false],
|
|
|
|
'ValueLessThanEqual with value below target' =>
|
|
|
|
['ValueLessThanEqual', '0', '-0.00001', true],
|
|
|
|
'ValueLessThanEqual with value equal to target' =>
|
|
|
|
['ValueLessThanEqual', '0', '0', true],
|
|
|
|
'ValueLessThanEqual with value greater to target' =>
|
|
|
|
['ValueLessThanEqual', '0', '0.0001', false],
|
|
|
|
'ValueGreaterThan with value below target' =>
|
|
|
|
['ValueGreaterThan', '0', '-0.00001', false],
|
|
|
|
'ValueGreaterThan with value equal to target' =>
|
|
|
|
['ValueGreaterThan', '0', '0', false],
|
|
|
|
'ValueGreaterThan with value greater to target' =>
|
|
|
|
['ValueGreaterThan', '0', '0.0001', true],
|
|
|
|
'ValueGreaterThanEqual with value below target' =>
|
|
|
|
['ValueGreaterThanEqual', '0', '-0.00001', false],
|
|
|
|
'ValueGreaterThanEqual with value equal to target' =>
|
|
|
|
['ValueGreaterThanEqual', '0', '0', true],
|
|
|
|
'ValueGreaterThanEqual with value greater to target' =>
|
|
|
|
['ValueGreaterThanEqual', '0', '0.0001', true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-05-08 07:02:01 +02:00
|
|
|
/**
|
|
|
|
* Test that methods are returned for manipulating the presence of the "hide" CSS class depending
|
|
|
|
* on whether the field should be hidden or shown
|
|
|
|
*/
|
2024-09-10 01:28:45 +02:00
|
|
|
#[DataProvider('dataProviderValidateAgainstFormData')]
|
2020-01-13 00:15:34 +01:00
|
|
|
public function testValidateAgainstFormData($condition, $targetValue, $value, $expected)
|
2018-05-08 07:02:01 +02:00
|
|
|
{
|
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
|
2020-01-13 00:15:34 +01:00
|
|
|
$rule1->ConditionOption = $condition;
|
|
|
|
$rule1->FieldValue = $targetValue;
|
2018-05-08 07:02:01 +02:00
|
|
|
|
2020-01-13 00:15:34 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$rule1->validateAgainstFormData([]),
|
|
|
|
'Unset value always returns false no matter the rule'
|
|
|
|
);
|
2018-05-08 07:02:01 +02:00
|
|
|
|
2020-01-13 00:15:34 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$rule1->validateAgainstFormData(['CountrySelection' => $value])
|
|
|
|
);
|
|
|
|
}
|
2018-05-08 07:02:01 +02:00
|
|
|
|
2020-01-13 00:15:34 +01:00
|
|
|
public function testValidateAgainstFormDataWithNonSenseRule()
|
|
|
|
{
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->expectException(\LogicException::class);
|
2020-01-13 00:15:34 +01:00
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
|
|
|
|
$rule1->ConditionOption = 'NonSenseRule';
|
|
|
|
$rule1->validateAgainstFormData(['CountrySelection' => 'booya']);
|
2018-05-08 07:02:01 +02:00
|
|
|
}
|
2017-05-22 05:54:28 +02:00
|
|
|
}
|