2013-03-15 01:56:43 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
|
|
|
use SilverStripe\ORM\ValidationException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
class ValidationExceptionTest extends SapphireTest
|
2013-03-15 01:56:43 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test that ValidationResult object can correctly populate a ValidationException
|
|
|
|
*/
|
|
|
|
public function testCreateFromValidationResult() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-22 22:29:15 +01:00
|
|
|
$result = new ValidationResult();
|
|
|
|
$result->addError('Not a valid result');
|
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$exception = new ValidationException($result);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals(0, $exception->getCode());
|
|
|
|
$this->assertEquals('Not a valid result', $exception->getMessage());
|
2016-11-22 22:29:15 +01:00
|
|
|
$this->assertFalse($exception->getResult()->valid());
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals('Not a valid result', $exception->getResult()->message());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Test that ValidationResult object with multiple errors can correctly
|
2013-03-15 01:56:43 +01:00
|
|
|
* populate a ValidationException
|
|
|
|
*/
|
|
|
|
public function testCreateFromComplexValidationResult() {
|
|
|
|
$result = new ValidationResult();
|
2016-11-22 22:29:15 +01:00
|
|
|
$result->addError('Invalid type')
|
|
|
|
->addError('Out of kiwis');
|
2013-03-15 01:56:43 +01:00
|
|
|
$exception = new ValidationException($result);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals(0, $exception->getCode());
|
|
|
|
$this->assertEquals('Invalid type; Out of kiwis', $exception->getMessage());
|
|
|
|
$this->assertEquals(false, $exception->getResult()->valid());
|
|
|
|
$this->assertEquals('Invalid type; Out of kiwis', $exception->getResult()->message());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Test that a ValidationException created with no contained ValidationResult
|
|
|
|
* will correctly populate itself with an inferred version
|
|
|
|
*/
|
|
|
|
public function testCreateFromMessage() {
|
|
|
|
$exception = new ValidationException('Error inferred from message', E_USER_ERROR);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals(E_USER_ERROR, $exception->getCode());
|
|
|
|
$this->assertEquals('Error inferred from message', $exception->getMessage());
|
2016-11-22 22:29:15 +01:00
|
|
|
$this->assertFalse($exception->getResult()->valid());
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals('Error inferred from message', $exception->getResult()->message());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Test that ValidationException can be created with both a ValidationResult
|
|
|
|
* and a custom message
|
|
|
|
*/
|
|
|
|
public function testCreateWithValidationResultAndMessage() {
|
2016-11-22 22:29:15 +01:00
|
|
|
$result = new ValidationResult();
|
|
|
|
$result->addError('Incorrect placement of cutlery');
|
2013-03-15 01:56:43 +01:00
|
|
|
$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals(E_USER_WARNING, $exception->getCode());
|
|
|
|
$this->assertEquals('An error has occurred', $exception->getMessage());
|
2016-11-22 22:29:15 +01:00
|
|
|
$this->assertFalse($exception->getResult()->valid());
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals('Incorrect placement of cutlery', $exception->getResult()->message());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Test that ValidationException can be created with both a ValidationResult
|
|
|
|
* and a custom message
|
|
|
|
*/
|
|
|
|
public function testCreateWithComplexValidationResultAndMessage() {
|
|
|
|
$result = new ValidationResult();
|
2016-11-22 22:29:15 +01:00
|
|
|
$result->addError('A spork is not a knife')
|
|
|
|
->addError('A knife is not a back scratcher');
|
2013-03-15 01:56:43 +01:00
|
|
|
$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
$this->assertEquals(E_USER_WARNING, $exception->getCode());
|
|
|
|
$this->assertEquals('An error has occurred', $exception->getMessage());
|
|
|
|
$this->assertEquals(false, $exception->getResult()->valid());
|
2013-08-21 08:54:05 +02:00
|
|
|
$this->assertEquals('A spork is not a knife; A knife is not a back scratcher',
|
|
|
|
$exception->getResult()->message());
|
2013-03-15 01:56:43 +01:00
|
|
|
}
|
2014-01-08 23:03:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test combining validation results together
|
|
|
|
*/
|
|
|
|
public function testCombineResults(){
|
|
|
|
$result = new ValidationResult();
|
|
|
|
$anotherresult = new ValidationResult();
|
|
|
|
$yetanotherresult = new ValidationResult();
|
2016-11-22 22:29:15 +01:00
|
|
|
$anotherresult->addError("Eat with your mouth closed", 'bad', "EATING101");
|
|
|
|
$yetanotherresult->addError("You didn't wash your hands", 'bad', "BECLEAN", false);
|
2014-01-08 23:03:36 +01:00
|
|
|
|
|
|
|
$this->assertTrue($result->valid());
|
|
|
|
$this->assertFalse($anotherresult->valid());
|
|
|
|
$this->assertFalse($yetanotherresult->valid());
|
|
|
|
|
|
|
|
$result->combineAnd($anotherresult)
|
|
|
|
->combineAnd($yetanotherresult);
|
|
|
|
$this->assertFalse($result->valid());
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"EATING101" => "Eat with your mouth closed",
|
|
|
|
"BECLEAN" => "You didn't wash your hands"
|
2016-11-22 22:29:15 +01:00
|
|
|
), $result->messageList());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a ValidationException created with no contained ValidationResult
|
|
|
|
* will correctly populate itself with an inferred version
|
|
|
|
*/
|
|
|
|
public function testCreateForField() {
|
|
|
|
$exception = ValidationException::create_for_field('Content', 'Content is required');
|
|
|
|
|
|
|
|
$this->assertEquals('Content is required', $exception->getMessage());
|
|
|
|
$this->assertEquals(false, $exception->getResult()->valid());
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Content' => array(
|
|
|
|
'message' => 'Content is required',
|
|
|
|
'messageType' => 'bad',
|
|
|
|
),
|
|
|
|
), $exception->getResult()->fieldErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a ValidationException created with no contained ValidationResult
|
|
|
|
* will correctly populate itself with an inferred version
|
|
|
|
*/
|
|
|
|
public function testValidationResultAddMethods() {
|
|
|
|
$result = new ValidationResult();
|
|
|
|
$result->addMessage('A spork is not a knife', 'bad');
|
|
|
|
$result->addError('A knife is not a back scratcher');
|
|
|
|
$result->addFieldMessage('Title', 'Title is good', 'good');
|
|
|
|
$result->addFieldError('Content', 'Content is bad');
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Title' => array(
|
|
|
|
'message' => 'Title is good',
|
|
|
|
'messageType' => 'good'
|
|
|
|
),
|
|
|
|
'Content' => array(
|
|
|
|
'message' => 'Content is bad',
|
|
|
|
'messageType' => 'bad'
|
|
|
|
)
|
|
|
|
), $result->fieldErrors());
|
|
|
|
|
|
|
|
$this->assertEquals('A spork is not a knife; A knife is not a back scratcher', $result->overallMessage());
|
|
|
|
|
|
|
|
$exception = ValidationException::create_for_field('Content', 'Content is required');
|
2014-01-08 23:03:36 +01:00
|
|
|
}
|
|
|
|
|
2013-08-21 08:54:05 +02:00
|
|
|
}
|