2015-05-16 01:46:54 +02:00
|
|
|
<?php
|
2016-08-19 10:51:35 +12:00
|
|
|
|
2016-10-14 14:30:05 +13:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2019-10-22 17:04:58 +13:00
|
|
|
use SilverStripe\Forms\Tip;
|
2024-10-10 19:59:07 +13:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2016-08-19 10:51:35 +12:00
|
|
|
|
2016-12-16 17:34:21 +13:00
|
|
|
class TextFieldTest extends SapphireTest
|
|
|
|
{
|
2015-05-16 01:46:54 +02:00
|
|
|
|
2016-12-16 17:34:21 +13:00
|
|
|
/**
|
|
|
|
* Tests the TextField Max Length Validation Failure
|
|
|
|
*/
|
|
|
|
public function testMaxLengthValidationFail()
|
|
|
|
{
|
|
|
|
$textField = new TextField('TestField');
|
|
|
|
$textField->setMaxLength(5);
|
|
|
|
$textField->setValue("John Doe"); // 8 characters, so should fail
|
|
|
|
$result = $textField->validate(new RequiredFields());
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2015-05-16 01:46:54 +02:00
|
|
|
|
2016-12-16 17:34:21 +13:00
|
|
|
/**
|
|
|
|
* Tests the TextField Max Length Validation Success
|
|
|
|
*/
|
|
|
|
public function testMaxLengthValidationSuccess()
|
|
|
|
{
|
|
|
|
$textField = new TextField('TestField');
|
|
|
|
$textField->setMaxLength(5);
|
|
|
|
$textField->setValue("John"); // 4 characters, so should pass
|
|
|
|
$result = $textField->validate(new RequiredFields());
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2019-10-22 17:04:58 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that when a Tip is applied to the field, it outputs it in the schema
|
|
|
|
*/
|
|
|
|
public function testTipIsIncludedInSchema()
|
|
|
|
{
|
|
|
|
$textField = new TextField('TestField');
|
|
|
|
$this->assertArrayNotHasKey('tip', $textField->getSchemaDataDefaults());
|
|
|
|
|
|
|
|
$textField->setTip(new Tip('TestTip'));
|
|
|
|
$this->assertArrayHasKey('tip', $textField->getSchemaDataDefaults());
|
|
|
|
}
|
2024-10-10 19:59:07 +13:00
|
|
|
|
|
|
|
public static function provideSetValue(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'string' => [
|
|
|
|
'value' => 'fish',
|
|
|
|
'expected' => 'fish',
|
|
|
|
],
|
|
|
|
'string-blank' => [
|
|
|
|
'value' => '',
|
|
|
|
'expected' => '',
|
|
|
|
],
|
|
|
|
'null' => [
|
|
|
|
'value' => null,
|
|
|
|
'expected' => '',
|
|
|
|
],
|
|
|
|
'zero' => [
|
|
|
|
'value' => 0,
|
|
|
|
'expected' => 0,
|
|
|
|
],
|
|
|
|
'true' => [
|
|
|
|
'value' => true,
|
|
|
|
'expected' => true,
|
|
|
|
],
|
|
|
|
'false' => [
|
|
|
|
'value' => false,
|
|
|
|
'expected' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[DataProvider('provideSetValue')]
|
|
|
|
public function testSetValue(mixed $value, mixed $expected): void
|
|
|
|
{
|
|
|
|
$field = new TextField('TestField');
|
|
|
|
$field->setValue($value);
|
|
|
|
$this->assertSame($expected, $field->getValue());
|
|
|
|
}
|
2015-05-16 01:46:54 +02:00
|
|
|
}
|