View source
<?php
namespace Drupal\Tests\feeds\Functional\Feeds\Target;
use Drupal\feeds\Feeds\Target\Password;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\Tests\feeds\Functional\FeedsBrowserTestBase;
class PasswordTest extends FeedsBrowserTestBase {
public static $modules = [
'feeds',
'user',
];
protected $feedType;
protected $userStorage;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createFeedTypeForCsv([
'name' => 'name',
'mail' => 'mail',
'status' => 'status',
], [
'processor' => 'entity:user',
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'name',
],
],
[
'target' => 'mail',
'map' => [
'value' => 'mail',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'status',
'map' => [
'value' => 'status',
],
],
],
]);
$this->userStorage = $this->container
->get('entity_type.manager')
->getStorage('user');
}
public function test($source, array $settings = []) {
$this->feedType
->addCustomSource($source, [
'value' => $source,
]);
$this->feedType
->addMapping([
'target' => 'pass',
'map' => [
'value' => $source,
],
'settings' => $settings,
]);
$this->feedType
->save();
$this->userStorage
->create([
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'temporary',
'status' => 1,
])
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/users.csv',
]);
$this
->batchImport($feed);
$this
->assertSession()
->pageTextContains('Created 2 users');
$this
->assertSession()
->pageTextContains('Updated 1 user');
$this
->feedsLoginUser('Morticia', 'mort');
$this
->feedsLoginUser('Fester', 'fest');
$this
->feedsLoginUser('Gomez', 'gome');
}
public function providerPasswordTypes() {
return [
'plain' => [
'source' => 'password',
],
'md5' => [
'source' => 'password_md5',
'settings' => [
'pass_encryption' => Password::PASS_MD5,
],
],
'sha512' => [
'source' => 'password_sha512',
'settings' => [
'pass_encryption' => Password::PASS_SHA512,
],
],
];
}
protected function feedsLoginUser($username, $password) {
$account = user_load_by_name($username);
$this
->assertNotEmpty($account, 'Imported user account loaded.');
$account->passRaw = $password;
$this
->drupalLogin($account);
}
}