2010-10-04 06:26:12 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\EmailField;
|
2016-10-14 03:30:05 +02:00
|
|
|
use Exception;
|
2021-10-27 04:39:47 +02:00
|
|
|
use PHPUnit\Framework\AssertionFailedError;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\EmailFieldTest\TestValidator;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class EmailFieldTest extends FunctionalTest
|
|
|
|
{
|
2010-10-04 06:26:12 +02:00
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_controllers = [
|
2016-12-16 05:34:21 +01:00
|
|
|
EmailFieldTest\TestController::class,
|
|
|
|
];
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Check the php validator for email addresses. We should be checking against RFC 5322 which defines email address
|
|
|
|
* syntax.
|
|
|
|
*
|
|
|
|
* @TODO
|
|
|
|
* - double quotes around the local part (before @) is not supported
|
|
|
|
* - special chars ! # $ % & ' * + - / = ? ^ _ ` { | } ~ are all valid in local part
|
|
|
|
* - special chars ()[]\;:,<> are valid in the local part if the local part is in double quotes
|
|
|
|
* - "." is valid in the local part as long as its not first or last char
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmailAddressSyntax()
|
|
|
|
{
|
|
|
|
$this->internalCheck("blah@blah.com", "Valid, simple", true);
|
|
|
|
$this->internalCheck("mr.o'connor+on-toast@blah.com", "Valid, special chars", true);
|
|
|
|
$this->internalCheck("", "Empty email", true);
|
|
|
|
$this->internalCheck("invalid", "Invalid, simple", false);
|
|
|
|
$this->internalCheck("invalid@name@domain.com", "Invalid, two @'s", false);
|
|
|
|
$this->internalCheck("invalid@domain", "Invalid, domain too simple", false);
|
|
|
|
$this->internalCheck("domain.but.no.user", "Invalid, no user part", false);
|
|
|
|
}
|
2010-10-04 06:26:12 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function internalCheck($email, $checkText, $expectSuccess)
|
|
|
|
{
|
|
|
|
$field = new EmailField("MyEmail");
|
|
|
|
$field->setValue($email);
|
2010-10-04 06:26:12 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$val = new TestValidator();
|
|
|
|
try {
|
|
|
|
$field->validate($val);
|
|
|
|
// If we expect failure and processing gets here without an exception, the test failed
|
|
|
|
$this->assertTrue($expectSuccess, $checkText . " (/$email/ passed validation, but not expected to)");
|
|
|
|
} catch (Exception $e) {
|
2021-10-27 04:39:47 +02:00
|
|
|
if ($e instanceof AssertionFailedError) {
|
2016-12-16 05:34:21 +01:00
|
|
|
// re-throw assertion failure
|
|
|
|
throw $e;
|
|
|
|
} elseif ($expectSuccess) {
|
|
|
|
$this->fail(
|
|
|
|
$checkText . ": " . $e->getMessage() . " (/$email/ did not pass validation, but was expected to)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-28 02:02:13 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Check that input type='email' fields are submitted by SimpleTest
|
|
|
|
*
|
|
|
|
* @see SimpleTagBuilder::_createInputTag()
|
|
|
|
*/
|
2018-06-01 07:47:03 +02:00
|
|
|
public function testEmailFieldPopulation()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$this->get('EmailFieldTest_Controller');
|
2018-06-01 07:47:03 +02:00
|
|
|
|
|
|
|
$response = $this->submitForm(
|
2016-12-16 05:34:21 +01:00
|
|
|
'Form_Form',
|
|
|
|
null,
|
2018-06-01 07:47:03 +02:00
|
|
|
['Email' => 'test@test.com']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2012-09-28 02:02:13 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('Test save was successful', $response->getBody());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2010-10-04 06:26:12 +02:00
|
|
|
}
|