Skip to content

Commit

Permalink
2016-07-15 AC: Completes unit tests for the modSimpleEmailForm::isEma…
Browse files Browse the repository at this point in the history
…ilAddress() method. Contributes to resolve issue #9.
  • Loading branch information
andrewscaya committed Jul 26, 2016
1 parent 2a8deca commit 060bfc5
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,4 +3,4 @@ Joomla Simple Email Form Module

[![Build Status](https://travis-ci.org/andrewscaya/mod_simpleemailform.svg?branch=master)](https://travis-ci.org/andrewscaya/mod_simpleemailform)

Code Coverage: 30.85% covered.
Code Coverage: 36.60% covered.
14 changes: 7 additions & 7 deletions helper.php
Expand Up @@ -900,12 +900,18 @@ public static function isEmailAddress($email)
$domain = substr($email, $atIndex + 1);
$local = substr($email, 0, $atIndex);

// Check Length of domain
// Check length of domain
$domainLen = strlen($domain);
if ($domainLen < 1 || $domainLen > 255) {
return false;
}

// Check length of local
$localLen = strlen($local);
if ($localLen < 1 || $localLen > 64) {
return false;
}

/*
* Check the local address
* We're a bit more conservative about what constitutes a "legal" address, that is, A-Za-z0-9!#$%&\'*+/=?^_`{|}~-
Expand All @@ -923,12 +929,6 @@ public static function isEmailAddress($email)
return true;
}

// Check Lengths
$localLen = strlen($local);
if ($localLen < 1 || $localLen > 64) {
return false;
}

// Check the domain
$domain_array = explode(".", rtrim($domain, '.'));
$regex = '/^[A-Za-z0-9-]{0,63}$/';
Expand Down
208 changes: 200 additions & 8 deletions test/modSimpleEmailFormTest.php
Expand Up @@ -59,8 +59,6 @@ protected function setUp()
$this->modSimpleEmailForm = new modSimpleEmailForm($params);

$this->modSimpleEmailFormReflection = new \ReflectionClass($this->modSimpleEmailForm);
$this->formatErrorMessageMethod = $this->modSimpleEmailFormReflection->getMethod('formatErrorMessage');
$this->formatErrorMessageMethod->setAccessible(true);
}

/**
Expand Down Expand Up @@ -138,22 +136,204 @@ public function testAllPhptFiles()
// }

/**
* Valid email address
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
// public function testIsEmailAddress()
// {
// // TODO Auto-generated modSimpleEmailFormTest::testIsEmailAddress()
// $this->markTestIncomplete("isEmailAddress test not implemented");
public function testIsEmailAddress()
{
$this->assertTrue($this->modSimpleEmailForm->isEmailAddress('test@localhost.localdomain'));
}

// modSimpleEmailForm::isEmailAddress(/* parameters */);
// }
/**
* Empty email address
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressEmptyAddress()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress(''));
}

/**
* Invalid email domain address with less than one character
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidDomainLessThanOne()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test@'));
}

/**
* Valid email domain address with 63 characters
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressValidDomainWith63()
{
$address = 'test@test.';

$i = 0;

while ($i < 63) {
$address .= 'a';

$i++;
}

$this->assertTrue($this->modSimpleEmailForm->isEmailAddress($address));
}

/**
* Invalid email domain address with more than 63 characters
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidDomainGreaterThan63()
{
$address = 'test@test.';

$i = 0;

while ($i < 64) {
$address .= 'a';

$i++;
}

$this->assertFalse($this->modSimpleEmailForm->isEmailAddress($address));
}

/**
* Invalid email domain address with empty part
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidDomainEmptyPart()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test@localhost..localdomain'));
}

/**
* Invalid email domain address that begins with a dash
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidDomainBeginsDash()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test@-localhost.localdomain'));
}

/**
* Invalid email domain address that ends with a dash
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidDomainEndsDash()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test@localhost.localdomain-'));
}

/**
* Email address with IP address as domain
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressWithIPDomain()
{
$this->assertTrue($this->modSimpleEmailForm->isEmailAddress('test@127.0.0.1'));
}

/**
* Invalid local email address that ends with a dot
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidLocalThatEndsWithDot()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test.@local'));
}

/**
* Invalid local email address with IP address as domain
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressInvalidLocalWithIPDomain()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('test.@127.0.0.1'));
}

/**
* Empty local email address
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressEmptyLocal()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('@localhost.localdomain'));
}

/**
* Local email address with space
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressLocalWithSpace()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress(' @localhost.localdomain'));
}

/**
* Greater than 64 characters long local email address
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressLocalGreaterThan64()
{
$address = '';

$i = 0;

while ($i < 65) {
$address .= 'a';

$i++;
}

$address .= '@localhost.localdomain';

$this->assertFalse($this->modSimpleEmailForm->isEmailAddress($address));
}

/**
* Empty local email address with IP address as domain
*
* Tests modSimpleEmailForm::isEmailAddress()
*/
public function testIsEmailAddressEmptyLocalWithIPDomain()
{
$this->assertFalse($this->modSimpleEmailForm->isEmailAddress('@127.0.0.1'));
}

protected function setformatErrorMessageMethodAccessible()
{
$this->formatErrorMessageMethod = $this->modSimpleEmailFormReflection->getMethod('formatErrorMessage');
$this->formatErrorMessageMethod->setAccessible(true);
}

//Three tests to check the function's behaviour with filenames
/**
* Tests modSimpleEmailForm::FormatErrorMessage()
*/
public function testFormatErrorMessageNoFn()
{
$this->setformatErrorMessageMethodAccessible();

$this->formatErrorMessageMethod = $this->modSimpleEmailFormReflection->getMethod('formatErrorMessage');
$this->formatErrorMessageMethod->setAccessible(true);

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->standardMessage)
Expand All @@ -169,6 +349,8 @@ public function testFormatErrorMessageNoFn()
*/
public function testFormatErrorMessageWithFn()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->standardMessage, $this->fn)
Expand All @@ -184,6 +366,8 @@ public function testFormatErrorMessageWithFn()
*/
public function testFormatErrorMessageInvalidFn()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->standardMessage, $this->fnSpace)
Expand All @@ -206,6 +390,8 @@ public function testFormatErrorMessageInvalidFn()
*/
public function testFormatErrornullMessageMessage()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->nullMessage, $this->fn)
Expand All @@ -227,6 +413,8 @@ public function testFormatErrornullMessageMessage()
*/
public function testFormatErrorMessageSpaceMessage()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->emptyMessage, $this->fn)
Expand All @@ -248,6 +436,8 @@ public function testFormatErrorMessageSpaceMessage()
*/
public function testFormatErrorMessageRealMessage()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->standardMessage, $this->fn)
Expand All @@ -268,6 +458,8 @@ public function testFormatErrorMessageRealMessage()
*/
public function testFormatErrorMessageColor()
{
$this->setformatErrorMessageMethodAccessible();

$message = $this->formatErrorMessageMethod->invokeArgs(
$this->modSimpleEmailForm,
array($this->color, $this->standardMessage, $this->fn)
Expand Down

0 comments on commit 060bfc5

Please sign in to comment.