/**
     * @group ZF2-130
     */
    public function testUseMxRecordsBasicInvalid()
    {
        $validator = new EmailAddress(array(
            'useMxCheck'        => true,
            'useDeepMxCheck'    => true
        ));

        $emailAddresses = array(
            '',
            'bob

            @domain.com',
            'bob jones@domain.com',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '"*****@*****.**',
            '*****@*****.**',
            'bob+domain.com',
            'bob.domain.com',
            'bob @domain.com',
            'bob@ domain.com',
            'bob @ domain.com',
            '*****@*****.**'
            );
        foreach ($emailAddresses as $input) {
            $this->assertFalse($validator->isValid($input), implode("\n", $this->validator->getMessages()) . $input);
        }
    }
Esempio n. 2
0
 /**
  * Trims and validates email
  * 
  * @param string $email
  * @return string
  * @throws Exception
  */
 public static function validateEmail($email)
 {
     $validator = new EmailAddress();
     if (!$validator->isValid((new StringTrim())->filter($email))) {
         throw new Exception(Json::encode($validator->getMessages()));
     }
     return $email;
 }
Esempio n. 3
0
 /**
  * {@inheritDoc}
  */
 public function validateEntity(EntityInterface $entity, ErrorStore $errorStore)
 {
     if (false == $entity->getName()) {
         $errorStore->addError('o:name', 'The name cannot be empty.');
     }
     $email = $entity->getEmail();
     $validator = new EmailAddress();
     if (!$validator->isValid($email)) {
         $errorStore->addValidatorMessages('o:email', $validator->getMessages());
     }
     if (!$this->isUnique($entity, ['email' => $email])) {
         $errorStore->addError('o:email', sprintf('The email "%s" is already taken.', $email));
     }
     if (false == $entity->getRole()) {
         $errorStore->addError('o:role', 'Users must have a role.');
     }
 }
Esempio n. 4
0
 /**
  * Constructor
  *
  * @param  string $email
  * @param  null|string $name
  * @throws Exception\InvalidArgumentException
  * @return Address
  */
 public function __construct($email, $name = null)
 {
     $emailAddressValidator = new EmailAddressValidator(Hostname::ALLOW_DNS | Hostname::ALLOW_LOCAL);
     if (!is_string($email) || empty($email)) {
         throw new Exception\InvalidArgumentException('Email must be a valid email address');
     }
     if (preg_match("/[\r\n]/", $email)) {
         throw new Exception\InvalidArgumentException('CRLF injection detected');
     }
     if (!$emailAddressValidator->isValid($email)) {
         $invalidMessages = $emailAddressValidator->getMessages();
         throw new Exception\InvalidArgumentException(array_shift($invalidMessages));
     }
     if (null !== $name) {
         if (!is_string($name)) {
             throw new Exception\InvalidArgumentException('Name must be a string');
         }
         if (preg_match("/[\r\n]/", $name)) {
             throw new Exception\InvalidArgumentException('CRLF injection detected');
         }
         $this->name = $name;
     }
     $this->email = $email;
 }
Esempio n. 5
0
   /**
     * Test changing hostname settings via EmailAddress object
     *
     * @return void
     */
    public function testHostnameSettings()
    {
        $validator = new Validator\EmailAddress();

        // Check no IDN matching
        $validator->getHostnameValidator()->setValidateIdn(false);
        $valuesExpected = array(
            array(false, array('name@b�rger.de', 'name@h�llo.de', 'name@h�llo.se'))
            );
        foreach ($valuesExpected as $element) {
            foreach ($element[1] as $input) {
                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
            }
        }

        // Check no TLD matching
        $validator->getHostnameValidator()->setValidateTld(false);
        $valuesExpected = array(
            array(true, array('*****@*****.**', '*****@*****.**', '*****@*****.**'))
            );
        foreach ($valuesExpected as $element) {
            foreach ($element[1] as $input) {
                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
            }
        }
    }
Esempio n. 6
0
 /**
  * @group ZF2-130
  */
 public function testUseMxCheckBasicValid()
 {
     $validator = new Validator\EmailAddress(array('useMxCheck' => true, 'useDeepMxCheck' => true));
     $emailAddresses = array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', "B.O'*****@*****.**", '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**');
     foreach ($emailAddresses as $input) {
         $this->assertTrue($validator->isValid($input), "{$input} failed to pass validation:\n" . implode("\n", $validator->getMessages()));
     }
 }
Esempio n. 7
0
 public function testIdenticalAndNonIdenticalMessagesReturned()
 {
     $validator = new EmailAddress();
     $this->assertFalse($validator->isValid('*****@*****.**'));
     $this->assertCount(3, $validator->getMessages());
     $this->assertArrayHasKey(EmailAddress::INVALID_HOSTNAME, $validator->getMessages());
     $this->assertArrayHasKey(Hostname::UNKNOWN_TLD, $validator->getMessages());
     $this->assertArrayHasKey(Hostname::LOCAL_NAME_NOT_ALLOWED, $validator->getMessages());
     $validator->setMessages(array(EmailAddress::INVALID_HOSTNAME => 'This is the same error message', Hostname::UNKNOWN_TLD => 'This is the same error message'));
     $this->assertFalse($validator->isValid('*****@*****.**'));
     $this->assertCount(2, $validator->getMessages());
     $this->assertArrayHasKey(EmailAddress::INVALID_HOSTNAME, $validator->getMessages());
     $this->assertArrayHasKey(Hostname::LOCAL_NAME_NOT_ALLOWED, $validator->getMessages());
 }