Add forms

This commit is contained in:
Romain Neutron
2013-04-30 20:30:17 +02:00
parent 518aab9e4a
commit 5e6d506d8d
20 changed files with 564 additions and 7 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\NewEmail;
class NewEmailTest extends \PhraseanetPHPUnitAbstract
{
public function testAnUnknownAddressIsNotAlreadyRegistered()
{
$constraint = new NewEmail(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('nonehere'));
}
public function testARegisteredAddressIsAlreadyRegistered()
{
$constraint = new NewEmail(self::$DI['app']);
$this->assertTrue($constraint->isAlreadyRegistered(self::$DI['user']->get_email()));
}
public function testNullIsNotAlreadyRegistered()
{
$constraint = new NewEmail(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('null'));
}
public function testBlankIsNotAlreadyRegistered()
{
$constraint = new NewEmail(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered(''));
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\NewEmailValidator;
class NewEmailValidatorTest extends \PhraseanetPHPUnitAbstract
{
/**
* @dataProvider provideValidationData
*/
public function testValidate($value, $alreadyRegistered)
{
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
$builder = $context
->expects($this->exactly($alreadyRegistered ? 1 : 0))
->method('addViolation');
if ($alreadyRegistered) {
$builder->with($this->isType('string'));
}
$validator = new NewEmailValidator();
$validator->initialize($context);
$constraint = $this->getConstraint();
$constraint
->expects($this->once())
->method('isAlreadyRegistered')
->with($value)
->will($this->returnValue($alreadyRegistered));
$validator->validate($value, $constraint);
}
public function provideValidationData()
{
return array(
array('romain@neutron.io', true),
array('romain@neutron.io', false),
array('', false),
array(null, false),
);
}
private function getConstraint()
{
return $this
->getMockBuilder('Alchemy\Phrasea\Form\Constraint\NewEmail')
->disableOriginalConstructor()
->getMock();
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\PasswordToken;
class PasswordTokenTest extends \PhraseanetPHPUnitAbstract
{
public function testInvalidTokenIsNotValid()
{
$random = $this
->getMockBuilder('random')
->disableOriginalConstructor()
->setMethods(array('helloToken'))
->getMock();
$token = \random::generatePassword();
$random
->expects($this->once())
->method('helloToken')
->with(self::$DI['app'], $token)
->will($this->throwException(new \Exception_NotFound('Token not found')));
$constraint = new PasswordToken(self::$DI['app'], $random);
$this->assertFalse($constraint->isValid($token));
}
public function testValidTokenIsValid()
{
$random = $this
->getMockBuilder('random')
->disableOriginalConstructor()
->setMethods(array('helloToken'))
->getMock();
$token = \random::generatePassword();
$random
->expects($this->once())
->method('helloToken')
->with(self::$DI['app'], $token)
->will($this->returnValue(array('usr_id' => mt_rand(), 'type' => \random::TYPE_PASSWORD)));
$constraint = new PasswordToken(self::$DI['app'], $random);
$this->assertTrue($constraint->isValid($token));
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Form\Constraint\PasswordTokenValidator;
class PasswordTokenValidatorTest extends \PhraseanetPHPUnitAbstract
{
/**
* @dataProvider provideValidationData
*/
public function testValidate($value, $isValid)
{
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
$builder = $context
->expects($this->exactly($isValid ? 1 : 0))
->method('addViolation');
if ($isValid) {
$builder->with($this->isType('string'));
}
$validator = new PasswordTokenValidator();
$validator->initialize($context);
$constraint = $this->getConstraint();
$constraint
->expects($this->once())
->method('isValid')
->with($value)
->will($this->returnValue($isValid));
$validator->validate($value, $constraint);
}
public function provideValidationData()
{
return array(
array(\random::generatePassword(), true),
array(\random::generatePassword(), false),
);
}
private function getConstraint()
{
return $this
->getMockBuilder('Alchemy\Phrasea\Form\Constraint\PasswordToken')
->disableOriginalConstructor()
->getMock();
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Alchemy\Tests\Phrasea\Form;
abstract class FormTestCase extends \PhraseanetPHPUnitAbstract
{
public function testBuildForm()
{
$form = $this->getForm();
$builder = $this
->getMockBuilder('Symfony\Component\Form\FormBuilder')
->disableOriginalConstructor()
->getMock();
$form->buildForm($builder, array('disabled' => false));
}
public function testGetName()
{
$form = $this->getForm();
$this->assertNull($form->getName());
}
abstract protected function getForm();
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaAuthenticationForm;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaAuthenticationFormTest extends FormTestCase
{
protected function getForm()
{
return new PhraseaAuthenticationForm();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaAuthenticationWithMappingForm;;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaAuthenticationWithMappingFormTest extends FormTestCase
{
protected function getForm()
{
return new PhraseaAuthenticationWithMappingForm();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaForgotPasswordForm;;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaForgotPasswordFormTest extends FormTestCase
{
protected function getForm()
{
return new PhraseaForgotPasswordForm();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaRecoverPasswordForm;;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaRecoverPasswordFormTest extends FormTestCase
{
protected function getForm()
{
return new PhraseaRecoverPasswordForm(self::$DI['app']);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaRegisterForm;;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaRegisterFormTest extends FormTestCase
{
protected function getForm()
{
$available = array(
'parameter' => array(
'type' => 'text',
'label' => 'Yollah !',
)
);
$params = array(
array(
'name' => 'parameter',
'required' => true
)
);
return new PhraseaRegisterForm(self::$DI['app'], $available, $params, new \Alchemy\Phrasea\Utilities\String\Camelizer());
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Login;
use Alchemy\Phrasea\Form\Login\PhraseaRenewPasswordForm;;
use Alchemy\Tests\Phrasea\Form\FormTestCase;
class PhraseaRenewPasswordFormTest extends FormTestCase
{
protected function getForm()
{
return new PhraseaRenewPasswordForm();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Alchemy\Tests\Phrasea\Form\Type;
use Alchemy\Phrasea\Form\Type\GeonameType;
class GeonameTypeTest extends \PhraseanetPHPUnitAbstract
{
public function testGetParent()
{
$geoname = new GeonameType();
$this->assertEquals('text', $geoname->getParent());
}
public function testGetName()
{
$geoname = new GeonameType();
$this->assertEquals('geoname', $geoname->getName());
}
}