2016-10-13 05:53:30 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2016-10-13 05:53:30 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-09-27 09:25:37 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2017-09-26 13:16:02 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-10-13 05:53:30 +02:00
|
|
|
|
|
|
|
class SapphireTestTest extends SapphireTest
|
|
|
|
{
|
2017-09-27 09:25:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideResolveFixturePath()
|
|
|
|
{
|
|
|
|
return [
|
2017-10-17 09:22:11 +02:00
|
|
|
'sameDirectory' => [
|
|
|
|
__DIR__ . '/CsvBulkLoaderTest.yml',
|
|
|
|
'./CsvBulkLoaderTest.yml',
|
|
|
|
'Could not resolve fixture path relative from same directory',
|
|
|
|
],
|
|
|
|
'filenameOnly' => [
|
|
|
|
__DIR__ . '/CsvBulkLoaderTest.yml',
|
|
|
|
'CsvBulkLoaderTest.yml',
|
|
|
|
'Could not resolve fixture path from filename only',
|
|
|
|
],
|
|
|
|
'parentPath' => [
|
|
|
|
dirname(__DIR__) . '/ORM/DataObjectTest.yml',
|
|
|
|
'../ORM/DataObjectTest.yml',
|
|
|
|
'Could not resolve fixture path from parent path',
|
|
|
|
],
|
|
|
|
'absolutePath' => [
|
|
|
|
dirname(__DIR__) . '/ORM/DataObjectTest.yml',
|
|
|
|
dirname(__DIR__) . '/ORM/DataObjectTest.yml',
|
|
|
|
'Could not relsolve fixture path from absolute path',
|
|
|
|
],
|
2017-09-27 09:25:37 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideResolveFixturePath
|
|
|
|
*/
|
2017-10-17 09:22:11 +02:00
|
|
|
public function testResolveFixturePath($expected, $path, $message)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-09-27 09:25:37 +02:00
|
|
|
$expected,
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->resolveFixturePath($path),
|
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2017-09-26 13:16:02 +02:00
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* @useDatabase
|
|
|
|
*/
|
2017-09-26 13:16:02 +02:00
|
|
|
public function testActWithPermission()
|
|
|
|
{
|
|
|
|
$this->logOut();
|
|
|
|
$this->assertFalse(Permission::check('ADMIN'));
|
|
|
|
$this->actWithPermission('ADMIN', function () {
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertTrue(Permission::check('ADMIN'), 'Member should now have ADMIN role');
|
2017-09-26 13:16:02 +02:00
|
|
|
// check nested actAs calls work as expected
|
|
|
|
Member::actAs(null, function () {
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertFalse(Permission::check('ADMIN'), 'Member should not act as ADMIN any more after reset');
|
2017-09-26 13:16:02 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* @useDatabase
|
|
|
|
*/
|
2017-09-26 13:16:02 +02:00
|
|
|
public function testCreateMemberWithPermission()
|
|
|
|
{
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertEmpty(
|
|
|
|
Member::get()->filter(['Email' => 'TESTPERM@example.org']),
|
|
|
|
'DB should not have the test member created when the test starts'
|
|
|
|
);
|
2017-09-26 13:16:02 +02:00
|
|
|
$this->createMemberWithPermission('TESTPERM');
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertCount(
|
|
|
|
1,
|
|
|
|
Member::get()->filter(['Email' => 'TESTPERM@example.org']),
|
|
|
|
'Database should now contain the test member'
|
|
|
|
);
|
2017-09-27 09:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideAllMatchingList()
|
|
|
|
*
|
|
|
|
* @param $match
|
|
|
|
* @param $itemsForList
|
2017-10-17 09:22:11 +02:00
|
|
|
*
|
2017-09-27 09:25:37 +02:00
|
|
|
* @testdox Has assertion assertListAllMatch
|
|
|
|
*/
|
2017-10-17 09:22:11 +02:00
|
|
|
public function testAssertListAllMatch($match, $itemsForList, $message)
|
2017-09-27 09:25:37 +02:00
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertListAllMatch($match, $list, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generate SS_List as this is not possible in dataProvider
|
|
|
|
*
|
|
|
|
* @param array $itemsForList
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
private function generateArrayListFromItems($itemsForList)
|
|
|
|
{
|
|
|
|
$list = ArrayList::create();
|
|
|
|
foreach ($itemsForList as $data) {
|
|
|
|
$list->push(Member::create($data));
|
|
|
|
}
|
|
|
|
return $list;
|
2017-09-27 09:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotMatchingList()
|
|
|
|
*
|
|
|
|
* @param $match
|
|
|
|
* @param $itemsForList
|
|
|
|
*
|
|
|
|
* @testdox assertion assertListAllMatch fails when not all items are matching
|
|
|
|
*
|
|
|
|
* @expectedException \PHPUnit_Framework_ExpectationFailedException
|
|
|
|
*/
|
|
|
|
public function testAssertListAllMatchFailsWhenNotMatchingAllItems($match, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
|
|
|
|
$this->assertListAllMatch($match, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualListsWithEmptyList()
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList
|
2017-10-17 09:22:11 +02:00
|
|
|
*
|
2017-09-27 09:25:37 +02:00
|
|
|
* @testdox Has assertion assertListContains
|
|
|
|
*/
|
|
|
|
public function testAssertListContains($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
$list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
|
|
|
|
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertListContains($matches, $list, 'The list does not contain the expected items');
|
2017-09-27 09:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotContainingList
|
|
|
|
* @testdox assertion assertListEquals fails on non equal Lists
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList array
|
|
|
|
*
|
|
|
|
* @expectedException \PHPUnit_Framework_ExpectationFailedException
|
|
|
|
*/
|
|
|
|
public function testAssertListContainsFailsIfListDoesNotContainMatch($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
$list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
|
|
|
|
|
|
|
|
$this->assertListContains($matches, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotContainingList
|
|
|
|
*
|
|
|
|
* @testdox Has assertion assertListNotContains
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList
|
|
|
|
*/
|
|
|
|
public function testAssertListNotContains($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertListNotContains($matches, $list, 'List contains forbidden items');
|
2017-09-27 09:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualLists
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList
|
2017-10-17 09:22:11 +02:00
|
|
|
*
|
2017-09-27 09:25:37 +02:00
|
|
|
* @testdox assertion assertListNotContains throws a exception when a matching item is found in the list
|
|
|
|
*
|
|
|
|
* @expectedException \PHPUnit_Framework_ExpectationFailedException
|
|
|
|
*/
|
|
|
|
public function testAssertListNotContainsFailsWhenListContainsAMatch($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
$list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
|
|
|
|
$list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
|
|
|
|
|
|
|
|
$this->assertListNotContains($matches, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualListsWithEmptyList()
|
|
|
|
* @testdox Has assertion assertListEquals
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList
|
|
|
|
*/
|
|
|
|
public function testAssertListEquals($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
|
2017-10-17 09:22:11 +02:00
|
|
|
$this->assertListEquals($matches, $list, 'Lists do not equal');
|
2017-09-27 09:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNonEqualLists
|
|
|
|
* @testdox assertion assertListEquals fails on non equal Lists
|
|
|
|
*
|
|
|
|
* @param $matches
|
|
|
|
* @param $itemsForList
|
|
|
|
*
|
|
|
|
* @expectedException \PHPUnit_Framework_ExpectationFailedException
|
|
|
|
*/
|
|
|
|
public function testAssertListEqualsFailsOnNonEqualLists($matches, $itemsForList)
|
|
|
|
{
|
|
|
|
$list = $this->generateArrayListFromItems($itemsForList);
|
|
|
|
|
|
|
|
$this->assertListEquals($matches, $list);
|
|
|
|
}
|
2016-10-13 05:53:30 +02:00
|
|
|
}
|