Source of file FieldValidationTest.php
Size: 3,871 Bytes - Last Modified: 2021-12-24T06:41:08+00:00
/var/www/docs.ssmods.com/process/src/tests/FieldValidationTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | <?php namespace Codem\DomainValidation; use SilverStripe\Dev\FunctionalTest; use PHPUnit_Framework_AssertionFailedError; use Exception; use SilverStripe\Forms\Validator; /** * Test validation of various addresses within {@link MxValidatedEmailField} */ class EmailFieldTest extends FunctionalTest { protected $valid_email_address= "devnull+gmailthing@codem.com.au";// valid domain has MX protected $invalid_mx_address = "test@example.com";// valid domain that has no MX protected $invalid_domain_email_address = "test@example.comau";// typo on the domain, but a valid email address protected $valid_domain = "codem.com.au"; protected $valid_example_domain = "example.com"; protected $invalid_domain = "example"; public function testEmailValidation() { $emails = [ $this->valid_email_address => true, $this->invalid_mx_address => false, $this->invalid_domain_email_address => false, ]; foreach($emails as $email => $expected_result) { $field = new MxValidatedEmailField("TestEmailAddress"); $field->setValue($email); $validator = new DomainValidation_Validator(); try { $result = $field->validate($validator); $error_string = ""; if(!$result) { $errors = $validator->getErrors(); if(!empty($errors)) { foreach($errors as $error) { $error_string .= $error['message'] . " "; } } } // assert that what we expect $this->assertTrue($result == $expected_result, trim($error_string)); } catch (PHPUnit_Framework_AssertionFailedError $e) { // catch assertTrue exception above throw $e; } catch (Exception $e) { // success should not throw an Exception if($expected_result) { $this->assertTrue(false, "Email {$email} was expected to pass but failed and validation threw an exception: {$e->getMessage()}"); } } } } public function testDomainValidation() { $domains = [ $this->valid_domain => true, $this->valid_example_domain => true, $this->invalid_domain => false, ]; foreach($domains as $domain => $expected_result) { $field = new ValidatedDomainField("TestDomain"); $field->setValue($domain); $validator = new DomainValidation_Validator(); try { $result = $field->validate($validator); $error_string = ""; if(!$result) { $errors = $validator->getErrors(); if(!empty($errors)) { foreach($errors as $error) { $error_string .= $error['message'] . " "; } } } // assert that what we expect $this->assertTrue($result == $expected_result, trim($error_string)); } catch (PHPUnit_Framework_AssertionFailedError $e) { // catch assertTrue exception above throw $e; } catch (Exception $e) { // success should not throw an Exception if($expected_result) { $this->assertTrue(false, "Domain {$domain} was expected to pass but failed and validation threw an exception: {$e->getMessage()}"); } } } } } /** * Validator for test */ class DomainValidation_Validator extends Validator { public function javascript() { } public function php($data) { } } |