silverstripe-framework/tests/php/Security/MemberCsvBulkLoaderTest.php

93 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Security\Tests;
use SilverStripe\ORM\DataObject;
2016-10-14 03:30:05 +02:00
use SilverStripe\Security\Group;
2016-06-23 01:37:22 +02:00
use SilverStripe\Security\MemberCsvBulkLoader;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\Dev\SapphireTest;
class MemberCsvBulkLoaderTest extends SapphireTest {
protected static $fixture_file = 'MemberCsvBulkLoaderTest.yml';
2014-08-15 08:53:05 +02:00
public function testNewImport() {
$loader = new MemberCsvBulkLoader();
2016-10-14 03:30:05 +02:00
$results = $loader->load(__DIR__ . '/MemberCsvBulkLoaderTest/MemberCsvBulkLoaderTest.csv');
$created = $results->Created()->toArray();
$this->assertEquals(count($created), 2);
$this->assertEquals($created[0]->Email, 'author1@test.com');
$this->assertEquals($created[1]->Email, 'author2@test.com');
}
2014-08-15 08:53:05 +02:00
public function testOverwriteExistingImport() {
$author1 = new Member();
$author1->FirstName = 'author1_first_old';
$author1->Email = 'author1@test.com';
$author1->write();
2014-08-15 08:53:05 +02:00
$loader = new MemberCsvBulkLoader();
2016-10-14 03:30:05 +02:00
$results = $loader->load(__DIR__ . '/MemberCsvBulkLoaderTest/MemberCsvBulkLoaderTest.csv');
$created = $results->Created()->toArray();
$this->assertEquals(count($created), 1);
$updated = $results->Updated()->toArray();
$this->assertEquals(count($updated), 1);
$this->assertEquals($created[0]->Email, 'author2@test.com');
$this->assertEquals($updated[0]->Email, 'author1@test.com');
$this->assertEquals($updated[0]->FirstName, 'author1_first');
}
2014-08-15 08:53:05 +02:00
public function testAddToPredefinedGroups() {
2016-10-14 03:30:05 +02:00
$existinggroup = $this->objFromFixture(Group::class, 'existinggroup');
2014-08-15 08:53:05 +02:00
$loader = new MemberCsvBulkLoader();
$loader->setGroups(array($existinggroup));
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$results = $loader->load(__DIR__ . '/MemberCsvBulkLoaderTest/MemberCsvBulkLoaderTest.csv');
2014-08-15 08:53:05 +02:00
$created = $results->Created()->toArray();
$this->assertEquals(1, count($created[0]->Groups()->column('ID')));
$this->assertContains($existinggroup->ID, $created[0]->Groups()->column('ID'));
$this->assertEquals(1, count($created[1]->Groups()->column('ID')));
$this->assertContains($existinggroup->ID, $created[1]->Groups()->column('ID'));
}
2014-08-15 08:53:05 +02:00
public function testAddToCsvColumnGroupsByCode() {
2016-10-14 03:30:05 +02:00
$existinggroup = $this->objFromFixture(Group::class, 'existinggroup');
2014-08-15 08:53:05 +02:00
$loader = new MemberCsvBulkLoader();
2016-10-14 03:30:05 +02:00
$results = $loader->load(__DIR__ . '/MemberCsvBulkLoaderTest/MemberCsvBulkLoaderTest_withGroups.csv');
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$newgroup = DataObject::get_one(Group::class, array(
'"Group"."Code"' => 'newgroup'
));
$this->assertEquals($newgroup->Title, 'newgroup');
2014-08-15 08:53:05 +02:00
$created = $results->Created()->toArray();
$this->assertEquals(1, count($created[0]->Groups()->column('ID')));
$this->assertContains($existinggroup->ID, $created[0]->Groups()->column('ID'));
$this->assertEquals(2, count($created[1]->Groups()->column('ID')));
$this->assertContains($existinggroup->ID, $created[1]->Groups()->column('ID'));
$this->assertContains($newgroup->ID, $created[1]->Groups()->column('ID'));
}
2014-08-15 08:53:05 +02:00
public function testCleartextPasswordsAreHashedWithDefaultAlgo() {
$loader = new MemberCsvBulkLoader();
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$results = $loader->load(__DIR__ . '/MemberCsvBulkLoaderTest/MemberCsvBulkLoaderTest_cleartextpws.csv');
2014-08-15 08:53:05 +02:00
$member = $results->Created()->First();
$memberID = $member->ID;
DataObject::flush_and_destroy_cache();
2016-10-14 03:30:05 +02:00
$member = DataObject::get_by_id(Member::class, $memberID);
// TODO Direct getter doesn't work, wtf!
$this->assertEquals(Security::config()->password_encryption_algorithm, $member->getField('PasswordEncryption'));
$result = $member->checkPassword('mypassword');
$this->assertTrue($result->isValid());
}
}