View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
class AuthenticationBasicAuthTest extends AuthenticationTestBase {
protected static $keyName = 'key_basic_auth';
protected function setUp() : void {
parent::setUp();
Role::load(AccountInterface::AUTHENTICATED_ROLE)
->grantPermission('entity_share_server_access_channels')
->save();
$plugin = $this
->createAuthenticationPlugin($this->adminUser, $this->remote);
$this->remote
->mergePluginConfig($plugin);
$this->remote
->save();
foreach ($this->channels as $channel) {
$authorized_users = $channel
->get('authorized_users');
$authorized_users = array_merge($authorized_users, [
$this->adminUser
->uuid(),
]);
$channel
->set('authorized_users', $authorized_users);
$channel
->save();
}
$this
->createKey();
$this
->postSetupFixture();
}
protected function getAdministratorPermissions() {
return [
'bypass node access',
] + parent::getAdministratorPermissions();
}
public function testImport() {
foreach (static::$filesData as $file_data) {
$this
->assertFalse(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been deleted.');
}
$this
->pullChannel('node_es_test_en');
$this
->checkCreatedEntities();
foreach (static::$filesData as $file_definition) {
$this
->assertTrue(file_exists($file_definition['uri']), 'The physical file ' . $file_definition['filename'] . ' has been pulled and recreated.');
$this
->assertEquals(file_get_contents($file_definition['uri']), $file_definition['file_content'], 'The content of physical file ' . $file_definition['filename'] . ' is correct.');
}
$this
->resetImportedContent();
foreach (static::$filesData as $file_data) {
$this->fileSystem
->delete($file_data['uri']);
}
unset($this->entitiesData['file']);
unset($this->entitiesData['node']['en']['es_test_node_import_published']['field_es_test_file']);
$plugin = $this
->createAuthenticationPlugin($this->channelUser, $this->remote);
$this->remote
->mergePluginConfig($plugin);
$this->remote
->save();
$this
->resetRemoteCaches();
$this
->prepareContent();
$channel_infos = $this->remoteManager
->getChannelsInfos($this->remote);
$this
->reimportChannel($channel_infos);
$entity_storage = $this->entityTypeManager
->getStorage('node');
$published = $entity_storage
->loadByProperties([
'uuid' => 'es_test_node_import_published',
]);
$this
->assertEquals(count($published), 1, 'The published node was imported.');
$not_published = $entity_storage
->loadByProperties([
'uuid' => 'es_test_node_import_not_published',
]);
$this
->assertEquals(count($not_published), 0, 'The unpublished node was not imported.');
$plugin = $this->remote
->getAuthPlugin();
$configuration = $plugin
->getConfiguration();
$configuration['data'] = [
'credential_provider' => 'key',
'storage_key' => static::$keyName,
];
$plugin
->setConfiguration($configuration);
$this->remote
->mergePluginConfig($plugin);
$this->remote
->save();
$this
->resetImportedContent();
$this
->resetRemoteCaches();
$this
->prepareContent();
$this
->reimportChannel($channel_infos);
$entity_storage = $this->entityTypeManager
->getStorage('node');
$published = $entity_storage
->loadByProperties([
'uuid' => 'es_test_node_import_published',
]);
$this
->assertEquals(count($published), 1, 'The published node was imported.');
$not_published = $entity_storage
->loadByProperties([
'uuid' => 'es_test_node_import_not_published',
]);
$this
->assertEquals(count($not_published), 0, 'The unpublished node was not imported.');
}
protected function createKey() {
$this
->createTestKey(static::$keyName, 'entity_share_basic_auth', 'config');
$username = $this->channelUser
->getAccountName();
$password = $this->channelUser->passRaw;
$key_value = <<<EOT
{
"username": "{<span class="php-variable">$username</span>}",
"password": "{<span class="php-variable">$password</span>}"
}
EOT;
$this->testKey
->setKeyValue($key_value);
$this->testKey
->save();
}
}