updated core and modules

This commit is contained in:
Bachir Soussi Chiadmi
2017-09-09 16:27:51 +02:00
parent 027aa99b32
commit 41863f872c
7810 changed files with 318922 additions and 83631 deletions
@@ -5,8 +5,8 @@ description: 'Test module for Profile.'
dependencies:
- profile
# Information added by Drupal.org packaging script on 2016-08-31
version: '8.x-1.0-alpha5+4-dev'
# Information added by Drupal.org packaging script on 2017-06-12
version: '8.x-1.0-beta1'
core: '8.x'
project: 'profile'
datestamp: 1472611474
datestamp: 1497287652
@@ -0,0 +1,148 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
use Drupal\user\Entity\User;
/**
* Tests "default" functionality via the UI.
*
* @group profile
*/
class ProfileDefaultTest extends ProfileTestBase {
/**
* Testing demo user 1.
*
* @var \Drupal\user\UserInterface
*/
public $user1;
/**
* Testing demo user 2.
*
* @var \Drupal\user\UserInterface
*/
public $user2;
/**
* Profile entity storage.
*
* @var \Drupal\profile\ProfileStorageInterface
*/
public $profileStorage;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'access user profiles',
'administer profile',
'administer profile types',
'access administration pages',
]);
$this->user1 = User::create([
'name' => $this->randomMachineName(),
'mail' => $this->randomMachineName() . '@example.com',
]);
$this->user1->save();
$this->user2 = User::create([
'name' => $this->randomMachineName(),
'mail' => $this->randomMachineName() . '@example.com',
]);
$this->user2->save();
}
/**
* Tests whether profile default on edit is working.
*/
public function testProfileEdit() {
$type = ProfileType::create([
'id' => $this->randomMachineName(),
'label' => $this->randomMachineName(),
'registration' => FALSE,
'roles' => [],
'multiple' => TRUE,
]);
$type->save();
$admin_user = $this->drupalCreateUser([
'administer profile',
'administer users',
]);
// Create new profiles.
$profile1 = Profile::create([
'type' => $type->id(),
'uid' => $this->user1->id(),
]);
$profile1->save();
$profile2 = Profile::create([
'type' => $type->id(),
'uid' => $this->user1->id(),
]);
$profile2->save();
// Verify that the first profile of this type is default.
$this->assertTrue($profile1->isDefault());
$this->assertFalse($profile2->isDefault());
$this->drupalLogin($admin_user);
$this->drupalGet($profile1->toUrl('edit-form')->toString());
$this->assertSession()->buttonNotExists('Save and make default');
$this->assertSession()->buttonExists('Save');
$this->drupalGet($profile2->toUrl('edit-form')->toString());
$this->assertSession()->buttonExists('Save');
$this->assertSession()->buttonExists('Save and make default');
$this->submitForm([], 'Save and make default');
\Drupal::entityTypeManager()->getStorage('profile')->resetCache([$profile1->id(), $profile2->id()]);
$this->assertFalse(Profile::load($profile1->id())->isDefault());
$this->assertTrue(Profile::load($profile2->id())->isDefault());
}
/**
* Tests the entity.profile.set_default route.
*/
public function testSetDefaultRoute() {
$this->type->setMultiple(TRUE)->save();
$id = $this->type->id();
$test_user = $this->drupalCreateUser([
"view own $id profile",
"create $id profile",
"update own $id profile",
"delete own $id profile",
]);
$profile1 = Profile::create([
'type' => $id,
'uid' => $test_user->id(),
'profile_fullname' => 'Frederick Pabst',
]);
$profile1->save();
$profile2 = Profile::create([
'type' => $id,
'uid' => $test_user->id(),
'profile_fullname' => 'Frederick Miller',
]);
$profile2->save();
$this->drupalLogin($test_user);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user->id(), 'profile_type' => $id]));
$this->assertSession()->pageTextContains('Frederick Pabst');
$this->assertSession()->pageTextContains('Frederick Miller');
$this->assertSession()->linkExists('Mark as default');
$this->getSession()->getPage()->clickLink('Mark as default');
$this->assertSession()->responseContains(new FormattableMarkup('The %label profile has been marked as default.', ['%label' => $profile2->label()]));
}
}
@@ -0,0 +1,121 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests profile field access functionality.
*
* @group profile
*/
class ProfileFieldAccessTest extends ProfileTestBase {
/**
* A test user.
*
* @var \Drupal\user\Entity\User
*/
protected $webUser;
/**
* A test user.
*
* @var \Drupal\user\Entity\User
*/
protected $otherUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'access user profiles',
'administer profile',
'administer profile types',
'administer profile fields',
'administer profile display',
]);
$user_permissions = [
'access user profiles',
"create {$this->type->id()} profile",
"update own {$this->type->id()} profile",
"view any {$this->type->id()} profile",
];
$this->webUser = $this->drupalCreateUser($user_permissions);
$this->otherUser = $this->drupalCreateUser($user_permissions);
}
/**
* Tests private profile field access.
*/
public function testPrivateField() {
$this->field->setThirdPartySetting('profile', 'profile_private', TRUE);
$this->field->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'profile_bio',
'entity_type' => 'profile',
'type' => 'text',
]);
$field_storage->save();
$bio_field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->type->id(),
'label' => 'Bio',
]);
$bio_field->save();
$this->display
->setComponent($bio_field->getName(), [
'type' => 'string',
'label' => 'above',
])
->save();
$this->form
->setComponent($bio_field->getName(), [
'type' => 'text_default',
'settings' => [],
])->save();
// Fill in a field value.
$this->drupalLogin($this->webUser);
$secret = $this->randomMachineName();
$not_secret = $this->randomMachineName();
$this->drupalGet("user/{$this->webUser->id()}/{$this->type->id()}");
$edit = [
'profile_fullname[0][value]' => $secret,
'profile_bio[0][value]' => $not_secret,
];
$this->assertSession()->buttonNotExists('Save and make default');
$this->submitForm($edit, 'Save');
/** @var \Drupal\profile\ProfileStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage('profile');
$web_user_profile = $storage->loadDefaultByUser($this->webUser, $this->type->id());
// Verify that the private field value appears for the profile owner.
$this->drupalGet($web_user_profile->toUrl());
$this->assertSession()->pageTextContains($secret);
$this->assertSession()->pageTextContains($not_secret);
// Verify that the private field value does not appear for other users.
$this->drupalLogin($this->otherUser);
$this->drupalGet($web_user_profile->toUrl());
$this->assertSession()->pageTextNotContains($secret);
$this->assertSession()->pageTextContains($not_secret);
// Verify that the private field value appears for the administrator.
$this->drupalLogin($this->adminUser);
$this->drupalGet($web_user_profile->toUrl());
$this->assertSession()->pageTextContains($secret);
$this->assertSession()->pageTextContains($not_secret);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Core\Url;
/**
* Tests that the help page for the module is available.
*
* @group profile
*/
class ProfileHelpTest extends ProfileTestBase {
public static $modules = ['help'];
/**
* Tests that the help page loads.
*/
public function testHelpPage() {
$user = $this->createUser(['access administration pages']);
$this->drupalLogin($user);
$this->drupalGet(Url::fromRoute('help.page', ['name' => 'profile'])->toString());
$this->assertSession()->pageTextContains('Types of profiles');
$this->assertSession()->pageTextContains('Creating profiles');
}
}
@@ -30,36 +30,44 @@ class ProfileRegisterFormTest extends ProfileTestBase {
->save();
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['view own test profile']);
$this->drupalGet('user/register');
// Verify that the additional profile field is attached and required.
$name = $this->randomMachineName();
$pass_raw = $this->randomMachineName();
// Use existing email to verify normal validation happens.
$edit = [
'name' => $name,
'mail' => $this->randomMachineName() . '@example.com',
'mail' => $this->adminUser->getEmail(),
'pass[pass1]' => $pass_raw,
'pass[pass2]' => $pass_raw,
];
$this->drupalPostForm('user/register', $edit, t('Create new account'));
$this->submitForm($edit, t('Create new account'));
$this->assertSession()->pageTextContains(new FormattableMarkup('@name field is required.', ['@name' => $this->field->getLabel()]));
$this->assertSession()->pageTextContains(new FormattableMarkup('The email address @email is already taken.', ['@email' => $this->adminUser->getEmail()]));
// Verify that we can register.
$edit['mail'] = $this->randomMachineName() . '@example.com';
$edit["entity_" . $id . "[$field_name][0][value]"] = $this->randomMachineName();
$this->drupalPostForm(NULL, $edit, t('Create new account'));
$this->submitForm($edit, t('Create new account'));
$this->assertSession()->pageTextContains(new FormattableMarkup('Registration successful. You are now logged in.', []));
$new_user = user_load_by_name($name);
$this->assertTrue($new_user->isActive(), 'New account is active after registration.');
/** @var \Drupal\profile\ProfileStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage('profile');
// Verify that a new profile was created for the new user ID.
$profile = $this->container->get('entity_type.manager')
->getStorage('profile')
->loadByUser($new_user, $this->type->id());
$profile = $storage->loadDefaultByUser($new_user, $this->type->id());
$this->assertEquals($profile->get($field_name)->value, $edit["entity_" . $id . "[$field_name][0][value]"], 'Field value found in loaded profile.');
// Verify that, as the first profile of this type for the user, it was set
// as default.
$this->assertTrue($profile->isDefault());
// Verify that the profile field value appears on the user account page.
$this->drupalGet('user');
$this->drupalGet($profile->toUrl()->toString());
$this->assertSession()->pageTextContains($edit["entity_" . $id . "[$field_name][0][value]"]);
}
@@ -0,0 +1,160 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\profile\Entity\ProfileType;
/**
* Tests using revisions with profiles.
*
* @group profile
*/
class ProfileRevisionTest extends ProfileTestBase {
/**
* Testing profile type, with use_revisions.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $useRevisionsType;
/**
* @inheritDoc
*/
protected function setUp() {
parent::setUp();
$use_revisions_type = ProfileType::create([
'id' => $this->randomMachineName(),
'label' => $this->randomMachineName(),
'use_revisions' => TRUE,
]);
$use_revisions_type->save();
$this->useRevisionsType = $use_revisions_type;
$use_revisions_fullname_field = FieldConfig::create([
'field_storage' => $this->field->getFieldStorageDefinition(),
'bundle' => $this->useRevisionsType->id(),
'label' => 'Full name',
]);
$use_revisions_fullname_field->save();
// Configure the default display.
$use_revisions_display = EntityViewDisplay::load("profile.{$this->useRevisionsType->id()}.default");
if (!$use_revisions_display) {
$use_revisions_display = EntityViewDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->useRevisionsType->id(),
'mode' => 'default',
'status' => TRUE,
]);
$use_revisions_display->save();
}
$use_revisions_display
->setComponent($this->field->getName(), ['type' => 'string'])
->save();
// Configure the default form.
$use_revisions_form = EntityFormDisplay::load("profile.{$this->useRevisionsType->id()}.default");
if (!$use_revisions_form) {
$use_revisions_form = EntityFormDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->useRevisionsType->id(),
'mode' => 'default',
'status' => TRUE,
]);
$this->form->save();
}
$use_revisions_form
->setComponent($this->field->getName(), [
'type' => 'string_textfield',
])->save();
}
/**
* Tests revision handling.
*/
public function testProfileRevisions() {
/** @var \Drupal\profile\ProfileStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage('profile');
$user = $this->createUser([
"create {$this->type->id()} profile",
"view own {$this->type->id()} profile",
"update own {$this->type->id()} profile",
]);
$this->drupalLogin($user);
$create_url = Url::fromRoute('entity.profile.type.user_profile_form', [
'user' => $this->loggedInUser->id(),
'profile_type' => $this->type->id(),
]);
$this->drupalGet($create_url);
$edit = [
'profile_fullname[0][value]' => $this->getRandomGenerator()->word(10),
];
$this->submitForm($edit, 'Save');
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->type->id());
$existing_profile_id = $profile->id();
$existing_revision_id = $profile->getRevisionId();
$create_url = Url::fromRoute('entity.profile.type.user_profile_form', [
'user' => $this->loggedInUser->id(),
'profile_type' => $this->type->id(),
]);
$this->drupalGet($create_url);
$edit = [
'profile_fullname[0][value]' => $this->getRandomGenerator()->word(10),
];
$this->submitForm($edit, 'Save');
$storage->resetCache([$profile->id()]);
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->type->id());
$this->assertEquals($existing_profile_id, $profile->id());
$this->assertEquals($existing_revision_id, $profile->getRevisionId());
$user = $this->createUser([
"create {$this->useRevisionsType->id()} profile",
"view own {$this->useRevisionsType->id()} profile",
"update own {$this->useRevisionsType->id()} profile",
]);
$this->drupalLogin($user);
$create_url = Url::fromRoute('entity.profile.type.user_profile_form', [
'user' => $this->loggedInUser->id(),
'profile_type' => $this->useRevisionsType->id(),
]);
$this->drupalGet($create_url);
$edit = [
'profile_fullname[0][value]' => $this->getRandomGenerator()->word(10),
];
$this->submitForm($edit, 'Save');
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->useRevisionsType->id());
$existing_profile_id = $profile->id();
$existing_revision_id = $profile->getRevisionId();
$create_url = Url::fromRoute('entity.profile.type.user_profile_form', [
'user' => $this->loggedInUser->id(),
'profile_type' => $this->useRevisionsType->id(),
]);
$this->drupalGet($create_url);
$edit = [
'profile_fullname[0][value]' => $this->getRandomGenerator()->word(10),
];
$this->submitForm($edit, 'Save');
$storage->resetCache([$profile->id()]);
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->useRevisionsType->id());
$this->assertEquals($existing_profile_id, $profile->id());
$this->assertNotEquals($existing_revision_id, $profile->getRevisionId());
}
}
@@ -0,0 +1,134 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
use Drupal\user\Entity\User;
/**
* Tests tab functionality of profiles.
*
* @group profile
*/
class ProfileTabTest extends ProfileTestBase {
public static $modules = ['profile', 'field_ui', 'text', 'block'];
/**
* Testing demo user 1.
*
* @var \Drupal\user\UserInterface
*/
public $user1;
/**
* Testing demo user 2.
*
* @var \Drupal\user\UserInterface
*/
public $user2;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'access user profiles',
'administer profile',
'administer profile types',
'access administration pages',
]);
}
/**
* Tests tabs in profile UI.
*/
public function testProfileTabs() {
$types_data = [
'profile_type_0' => ['label' => $this->randomMachineName()],
'profile_type_1' => ['label' => $this->randomMachineName()],
];
/** @var ProfileType[] $types */
$types = [];
foreach ($types_data as $id => $values) {
$types[$id] = ProfileType::create(['id' => $id] + $values);
$types[$id]->save();
}
$this->container->get('router.builder')->rebuild();
$this->user1 = User::create([
'name' => $this->randomMachineName(),
'mail' => $this->randomMachineName() . '@example.com',
]);
$this->user1->save();
$this->user2 = User::create([
'name' => $this->randomMachineName(),
'mail' => $this->randomMachineName() . '@example.com',
]);
$this->user2->save();
// Create new profiles.
$profile1 = Profile::create($expected = [
'type' => $types['profile_type_0']->id(),
'uid' => $this->user1->id(),
]);
$profile1->save();
$profile2 = Profile::create($expected = [
'type' => $types['profile_type_1']->id(),
'uid' => $this->user2->id(),
]);
$profile2->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/config');
$this->clickLink('User profiles');
$this->assertResponse(200);
$this->assertUrl('admin/config/people/profiles');
$this->assertLink($profile1->label());
$this->assertLinkByHref($profile2->toUrl('canonical')->toString());
$tasks = [
['entity.profile.collection', []],
['entity.profile_type.collection', []],
];
$this->assertLocalTasks($tasks, 0);
}
/**
* Asserts local tasks in the page output.
*
* @param array $routes
* A list of expected local tasks, prepared as an array of route names and
* their associated route parameters, to assert on the page (in the given
* order).
* @param int $level
* (optional) The local tasks level to assert; 0 for primary, 1 for
* secondary. Defaults to 0.
*/
protected function assertLocalTasks(array $routes, $level = 0) {
$elements = $this->xpath('//*[contains(@class, :class)]//a', [
':class' => $level == 0 ? 'tabs primary' : 'tabs secondary',
]);
$this->assertTrue(count($elements), 'Local tasks found.');
foreach ($routes as $index => $route_info) {
list($route_name, $route_parameters) = $route_info;
$expected = Url::fromRoute($route_name, $route_parameters)->toString();
$method = ($elements[$index]->getAttribute('href') == $expected ? 'pass' : 'fail');
$this->{$method}(new FormattableMarkup('Task @number href @value equals @expected.', [
'@number' => $index + 1,
'@value' => (string) $elements[$index]->getAttribute('href'),
'@expected' => $expected,
]));
}
}
}
@@ -0,0 +1,127 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\profile\Entity\Profile;
use Drupal\user\Entity\User;
/**
* Tests various CRUD for Profile entity in browser.
*
* @group profile
*/
class ProfileTest extends ProfileTestBase {
/**
* Tests creating and editing a profile.
*/
public function testCreateEditProfile() {
$this->drupalLogin($this->adminUser);
$profile_fullname = $this->randomString();
$create_url = Url::fromRoute('entity.profile.type.user_profile_form', [
'user' => $this->loggedInUser->id(),
'profile_type' => $this->type->id(),
]);
$this->drupalGet($create_url->toString());
$this->assertSession()->titleEquals("Create {$this->type->label()} | Drupal");
$this->assertSession()->buttonNotExists('Save and make default');
$edit = [
'profile_fullname[0][value]' => $profile_fullname,
];
$this->submitForm($edit, 'Save');
/** @var \Drupal\profile\ProfileStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage('profile');
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->type->id());
$this->assertEquals($profile_fullname, $profile->get('profile_fullname')->value);
$this->drupalGet($profile->toUrl('edit-form')->toString());
$this->assertSession()->titleEquals("Edit {$this->type->label()} profile #{$profile->id()} | Drupal");
$profile_fullname = $this->randomString();
$edit = [
'profile_fullname[0][value]' => $profile_fullname,
];
$this->submitForm($edit, 'Save');
$storage->resetCache([$profile->id()]);
$profile = $storage->loadDefaultByUser($this->loggedInUser, $this->type->id());
$this->assertEquals($profile_fullname, $profile->get('profile_fullname')->value);
}
/**
* Tests that a profile belonging to an anonymous user can be edited.
*/
public function testAnonymousProfileEdit() {
$profile = $this->createProfile($this->type, User::getAnonymousUser());
$this->drupalLogin($this->adminUser);
$this->drupalGet($profile->toUrl('edit-form')->toString());
$profile_fullname = $this->randomString();
$edit = [
'profile_fullname[0][value]' => $profile_fullname,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->addressEquals($profile->toUrl('collection')->toString());
}
/**
* Tests entity.profile.type.user_profile_form route access.
*/
public function testUserProfileFormAccess() {
$id = $this->type->id();
$test_user1 = $this->drupalCreateUser([
"view own $id profile",
"create $id profile",
"update own $id profile",
"delete own $id profile",
]);
$test_user2 = $this->drupalCreateUser([
"view own $id profile",
"create $id profile",
"update own $id profile",
"delete own $id profile",
]);
$this->drupalLogin($this->adminUser);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user1->id(), 'profile_type' => $id]));
$this->assertSession()->statusCodeEquals(200);
$this->drupalLogin($test_user1);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user1->id(), 'profile_type' => $id]));
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user2->id(), 'profile_type' => $id]));
$this->assertSession()->statusCodeEquals(403);
$this->type->setMultiple(TRUE)->save();
// Add link only appears if there are existing profiles.
Profile::create([
'type' => $this->type->id(),
'uid' => $test_user1->id(),
'profile_fullname' => $this->randomMachineName(),
])->save();
Profile::create([
'type' => $this->type->id(),
'uid' => $test_user2->id(),
'profile_fullname' => $this->randomMachineName(),
])->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user1->id(), 'profile_type' => $id]));
$this->assertSession()->linkExists(new FormattableMarkup('Add new @type', ['@type' => $this->type->label()]));
$this->assertSession()->statusCodeEquals(200);
$this->drupalLogin($test_user1);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user1->id(), 'profile_type' => $id]));
$this->assertSession()->linkExists(new FormattableMarkup('Add new @type', ['@type' => $this->type->label()]));
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet(Url::fromRoute('entity.profile.type.user_profile_form', ['user' => $test_user2->id(), 'profile_type' => $id]));
$this->assertSession()->linkNotExists(new FormattableMarkup('Add new @type', ['@type' => $this->type->label()]));
$this->assertSession()->statusCodeEquals(403);
}
}
@@ -8,7 +8,6 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\profile\ProfileTestTrait;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
@@ -30,14 +29,14 @@ abstract class ProfileTestBase extends BrowserTestBase {
/**
* Testing profile type entity view display.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $display;
/**
* Testing profile type entity form display.
*
* @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form
* @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface
*/
protected $form;
@@ -98,7 +97,7 @@ abstract class ProfileTestBase extends BrowserTestBase {
->setComponent($this->field->getName(), ['type' => 'string'])
->save();
// Configure rhe default form.
// Configure the default form.
$this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default");
if (!$this->form) {
$this->form = EntityFormDisplay::create([
@@ -116,12 +115,11 @@ abstract class ProfileTestBase extends BrowserTestBase {
$this->checkPermissions([
'administer profile types',
"create $id profile",
"view own $id profile",
"view any $id profile",
"add own $id profile",
"add any $id profile",
"edit own $id profile",
"edit any $id profile",
"update own $id profile",
"update any $id profile",
"delete own $id profile",
"delete any $id profile",
]);
@@ -129,10 +127,10 @@ abstract class ProfileTestBase extends BrowserTestBase {
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access user profiles']);
$this->adminUser = $this->drupalCreateUser([
'administer profile types',
'administer profiles',
'administer profile',
"view any $id profile",
"add any $id profile",
"edit any $id profile",
"create $id profile",
"update any $id profile",
"delete any $id profile",
]);
}
@@ -0,0 +1,82 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\profile\Entity\ProfileType;
/**
* Tests basic CRUD functionality of profile types.
*
* @group profile
*/
class ProfileTypeCRUDTest extends ProfileTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'access user profiles',
'administer profile types',
'administer profile fields',
'administer profile display',
]);
}
/**
* Verify that routes are created for the profile type.
*/
public function testRoutes() {
$this->drupalLogin($this->adminUser);
$type = $this->createProfileType($this->randomMachineName());
\Drupal::service('router.builder')->rebuildIfNeeded();
$this->drupalGet("user/{$this->adminUser->id()}/{$type->id()}");
$this->assertResponse(200);
}
/**
* Tests CRUD operations for profile types through the UI.
*/
public function testUi() {
$this->drupalLogin($this->adminUser);
// Create a new profile type.
$this->drupalGet('admin/config/people/profiles/types');
$this->assertResponse(200);
$this->clickLink(t('Add profile type'));
$this->assertUrl('admin/config/people/profiles/types/add');
$id = Unicode::strtolower($this->randomMachineName());
$label = $this->getRandomGenerator()->word(10);
$edit = [
'id' => $id,
'label' => $label,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertUrl('admin/config/people/profiles/types');
$this->assertRaw(new FormattableMarkup('%label profile type has been created.', ['%label' => $label]));
$this->assertLinkByHref("admin/config/people/profiles/types/manage/$id");
$this->assertLinkByHref("admin/config/people/profiles/types/manage/$id/fields");
$this->assertLinkByHref("admin/config/people/profiles/types/manage/$id/display");
$this->assertLinkByHref("admin/config/people/profiles/types/manage/$id/delete");
// Edit the new profile type.
$this->drupalGet("admin/config/people/profiles/types/manage/$id");
$this->assertRaw(new FormattableMarkup('Edit %label profile type', ['%label' => $label]));
$this->getSession()->getPage()->checkField('Include in user registration form');
$this->getSession()->getPage()->checkField('Create a new revision when a profile is modified');
$this->submitForm([], 'Save');
$this->assertUrl('admin/config/people/profiles/types');
$this->assertRaw(new FormattableMarkup('%label profile type has been updated.', ['%label' => $label]));
$profile_type = ProfileType::load($id);
$this->assertEquals($label, $profile_type->label());
$this->assertTrue($profile_type->getRegistration());
$this->assertTrue($profile_type->shouldCreateNewRevision());
}
}
@@ -1,10 +1,12 @@
<?php
namespace Drupal\profile\Tests;
namespace Drupal\Tests\profile\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\profile\Entity\Profile;
use Drupal\profile\ProfileTestTrait;
use Drupal\user\Entity\User;
/**
* Tests profile access handling.
@@ -20,7 +22,11 @@ class ProfileAccessTest extends EntityKernelTestBase {
*
* @var array
*/
public static $modules = ['user', 'system', 'field', 'text', 'profile', 'views'];
public static $modules = [
'entity',
'profile',
'views',
];
/**
* The access control handler.
@@ -52,7 +58,8 @@ class ProfileAccessTest extends EntityKernelTestBase {
$this->installEntitySchema('profile');
$this->installEntitySchema('view');
$this->installConfig(['profile']);
$this->accessControlHandler = $this->container->get('entity_type.manager')->getAccessControlHandler('profile');
$this->accessControlHandler = $this->container->get('entity_type.manager')
->getAccessControlHandler('profile');
$this->accessManager = $this->container->get('access_manager');
$this->type = $this->createProfileType('test', 'Test profile', TRUE);
$this->createUser();
@@ -71,14 +78,16 @@ class ProfileAccessTest extends EntityKernelTestBase {
// Verify access through route.
$this->assertFalse($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
'entity.profile.type.user_profile_form.add',
['user' => $web_user1->id(), 'profile_type' => $this->type->id()],
$web_user1
));
// Test user with permission to only add own profile.
$web_user2 = $this->createUser([], ["add own {$this->type->id()} profile"]);
// Test user with permission to only add a profile.
$web_user2 = $this->createUser([], [
"create {$this->type->id()} profile",
"update own {$this->type->id()} profile",
]);
$web_user1->addRole($web_user2->get('roles')->first()->target_id);
$web_user1->save();
@@ -86,15 +95,15 @@ class ProfileAccessTest extends EntityKernelTestBase {
$access = $this->accessControlHandler->createAccess($this->type->id(), $web_user2);
$this->assertTrue($access);
// User 2 can create own profile of type.
// User 2 can create profile of type.
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
'entity.profile.type.user_profile_form.add',
['user' => $web_user2->id(), 'profile_type' => $this->type->id()],
$web_user2
));
// User 1 cannot create a profile for User 2.
$this->assertFalse($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
'entity.profile.type.user_profile_form.add',
['user' => $web_user2->id(), 'profile_type' => $this->type->id()],
$web_user1
));
@@ -103,23 +112,26 @@ class ProfileAccessTest extends EntityKernelTestBase {
$second_type = $this->createProfileType('test2', 'Test2 profile', TRUE);
$this->assertFalse($this->accessControlHandler->createAccess('test2', $web_user2));
$this->assertFalse($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
'entity.profile.type.user_profile_form.add',
['user' => $web_user2->id(), 'profile_type' => $second_type->id()],
$web_user2
));
// Test user with permission to only add any profile.
$web_user3 = $this->createUser([], ["add any {$this->type->id()} profile"]);
$web_user3 = $this->createUser([], [
"create {$this->type->id()} profile",
"update own {$this->type->id()} profile",
]);
$this->assertTrue($this->accessControlHandler->createAccess($this->type->id(), $web_user3));
// Verify access through route.
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
'entity.profile.type.user_profile_form.add',
['user' => $web_user3->id(), 'profile_type' => $this->type->id()],
$web_user3
));
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
$this->assertFalse($this->accessManager->checkNamedRoute(
'entity.profile.type.user_profile_form.add',
['user' => $web_user2->id(), 'profile_type' => $this->type->id()],
$web_user3
));
@@ -159,17 +171,14 @@ class ProfileAccessTest extends EntityKernelTestBase {
$this->assertTrue($profile1->access('view', $web_user2));
$this->assertTrue($profile2->access('view', $web_user2));
$user_view_builder = $this->container->get('entity_type.manager')->getViewBuilder('user');
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ["view own {$this->type->id()} profile"]);
$this->assertFalse($profile1->access('view', User::getAnonymousUser()));
$this->assertFalse($profile2->access('view', User::getAnonymousUser()));
$this->container->get('current_user')->setAccount($web_user1);
$user2_view = $user_view_builder->view($web_user2);
$this->render($user2_view);
$this->assertNoText($this->type->label());
$this->container->get('current_user')->setAccount($web_user2);
$user1_view = $user_view_builder->view($web_user1);
$this->render($user1_view);
$this->assertText($this->type->label());
// @todo Fix in https://www.drupal.org/node/2820209
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ["view any {$this->type->id()} profile"]);
// $this->assertTrue($profile1->access('view', User::getAnonymousUser()));
// $this->assertTrue($profile2->access('view', User::getAnonymousUser()));
}
/**
@@ -178,12 +187,12 @@ class ProfileAccessTest extends EntityKernelTestBase {
public function testProfileEditAccess() {
// Setup users.
$web_user1 = $this->createUser([], [
"add own {$this->type->id()} profile",
"edit own {$this->type->id()} profile",
"create {$this->type->id()} profile",
"update own {$this->type->id()} profile",
]);
$web_user2 = $this->createUser([], [
"add any {$this->type->id()} profile",
"edit any {$this->type->id()} profile",
"create {$this->type->id()} profile",
"update any {$this->type->id()} profile",
]);
// Setup profiles.
@@ -199,12 +208,12 @@ class ProfileAccessTest extends EntityKernelTestBase {
$profile2->save();
// Test user1 can only edit own profiles.
$this->assertTrue($profile1->access('edit', $web_user1));
$this->assertFalse($profile2->access('edit', $web_user1));
$this->assertTrue($profile1->access('update', $web_user1));
$this->assertFalse($profile2->access('update', $web_user1));
// Test user2 can edit any profiles.
$this->assertTrue($profile1->access('edit', $web_user2));
$this->assertTrue($profile2->access('edit', $web_user2));
$this->assertTrue($profile1->access('update', $web_user2));
$this->assertTrue($profile2->access('update', $web_user2));
}
}
@@ -0,0 +1,201 @@
<?php
namespace Drupal\Tests\profile\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\profile\ProfileTestTrait;
use Drupal\user\Entity\Role;
/**
* Tests profile role access handling.
*
* @group profile
*/
class ProfileRoleAccessTest extends EntityKernelTestBase {
use ProfileTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'entity',
'profile',
'views',
];
/**
* Randomly generated profile type entity.
*
* No roles.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $type1;
/**
* Randomly generated profile type entity.
*
* Requires some, but not all roles.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $type2;
/**
* Randomly generated profile type entity.
*
* Requires all profile roles.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $type3;
/**
* Randomly generated user role entity.
*
* @var \Drupal\user\Entity\Role
*/
protected $role1;
/**
* Randomly generated user role entity.
*
* @var \Drupal\user\Entity\Role
*/
protected $role2;
/**
* The profile access handler.
*
* @var \Drupal\profile\ProfileAccessControlHandler
*/
protected $accessHandler;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->role1 = Role::create([
'id' => strtolower($this->randomMachineName(8)),
'label' => $this->randomMachineName(8),
]);
$this->role1->save();
$this->role2 = Role::create([
'id' => strtolower($this->randomMachineName(8)),
'label' => $this->randomMachineName(8),
]);
$this->role2->save();
$this->type1 = $this->createProfileType(NULL, NULL, FALSE, []);
$this->type2 = $this->createProfileType(NULL, NULL, FALSE, [$this->role2->id()]);
$this->type3 = $this->createProfileType(NULL, NULL, FALSE, [
$this->role1->id(),
$this->role2->id(),
]);
$this->accessHandler = $this->container->get('entity_type.manager')
->getAccessControlHandler('profile');
// Do not allow uid == 1 to skew tests.
$this->createUser();
}
/**
* Tests profile form access for a type that has no role requirement.
*/
public function testProfileWithNoRoles() {
// Create user with add profile permissions.
$web_user1 = $this->createUser([], ["create {$this->type1->id()} profile"]);
$this->assertTrue($this->accessHandler->createAccess($this->type1->id(), $web_user1));
}
/**
* Tests profile form access for a type that has the locked role requirement.
*/
public function testLockedRoles() {
$locked_role_type = $this->createProfileType(NULL, NULL, FALSE, [AccountInterface::AUTHENTICATED_ROLE]);
// Create user with add profile permissions.
$web_user1 = $this->createUser([], ["create {$locked_role_type->id()} profile"]);
$this->assertTrue($this->accessHandler->createAccess($locked_role_type->id(), $web_user1));
}
/**
* Tests profile form access for a type that requires a role.
*/
public function testProfileWithSingleRole() {
// Create user with add own profile permissions.
$web_user1 = $this->createUser([], ["create {$this->type2->id()} profile"]);
// Test user without role can access add profile form.
// Expected: User cannot access form.
$this->assertFalse($this->accessHandler->createAccess($this->type2->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user with wrong role can access add profile form.
// Expected: User cannot access form.
$web_user1->addRole($this->role1->id());
$web_user1->save();
$this->assertFalse($this->accessHandler->createAccess($this->type2->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user with correct role can access add profile form.
// Expected: User can access form.
$web_user1->removeRole($this->role1->id());
$web_user1->addRole($this->role2->id());
$web_user1->save();
$this->reloadEntity($web_user1);
$this->assertTrue($this->accessHandler->createAccess($this->type2->id(), $web_user1));
}
/**
* Tests profile form access for a type that requires multiple roles.
*/
public function testProfileWithAllRoles() {
// Create user with add own profile permissions.
$web_user1 = $this->createUser([], ["create {$this->type3->id()} profile"]);
// Test user without role can access add profile form.
// Expected: User cannot access form.
$this->assertFalse($this->accessHandler->createAccess($this->type3->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user with role 1 can access add profile form.
// Expected: User can access form.
$web_user1->addRole($this->role1->id());
$web_user1->save();
$this->assertTrue($this->accessHandler->createAccess($this->type3->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user with both roles can access add profile form.
// Expected: User can access form.
$web_user1->addRole($this->role2->id());
$web_user1->save();
$this->assertTrue($this->accessHandler->createAccess($this->type3->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user with role 2 can access add profile form.
// Expected: User can access form.
$web_user1->removeRole($this->role1->id());
$web_user1->save();
$this->assertTrue($this->accessHandler->createAccess($this->type3->id(), $web_user1));
$this->accessHandler->resetCache();
// Test user without role can access add profile form.
// Expected: User cannot access form.
$web_user1->removeRole($this->role2->id());
$web_user1->save();
$this->assertFalse($this->accessHandler->createAccess($this->type3->id(), $web_user1));
}
}
@@ -3,9 +3,11 @@
namespace Drupal\Tests\profile\Kernel;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
use Drupal\profile\ProfileTestTrait;
/**
* Tests basic functionality of profiles.
@@ -20,10 +22,7 @@ class ProfileTest extends EntityKernelTestBase {
* @var array
*/
public static $modules = [
'user',
'system',
'field',
'text',
'entity',
'profile',
'views',
];
@@ -38,7 +37,7 @@ class ProfileTest extends EntityKernelTestBase {
/**
* Testing demo user 2.
*
* @var \Drupal\user\UserInterface;
* @var \Drupal\user\UserInterface
*/
public $user2;
@@ -80,21 +79,13 @@ class ProfileTest extends EntityKernelTestBase {
$types[$id]->save();
}
// Create a new profile.
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = $this->profileStorage->create($expected = [
$profile = $this->profileStorage->create([
'type' => $types['profile_type_0']->id(),
'uid' => $this->user1->id(),
]);
$expected_label = new TranslatableMarkup('@type profile of @username (uid: @uid)', [
'@type' => $types['profile_type_0']->label(),
'@username' => $this->user1->getDisplayName(),
'@uid' => $this->user1->id(),
]);
$this->assertEquals($expected_label, $profile->label());
$this->assertEquals($profile->getOwnerId(), $this->user1->id());
$this->assertEquals($profile->getCreatedTime(), REQUEST_TIME);
$this->assertEquals($profile->getChangedTime(), REQUEST_TIME);
@@ -102,6 +93,11 @@ class ProfileTest extends EntityKernelTestBase {
// Save the profile.
$profile->save();
$this->assertEquals(REQUEST_TIME, $profile->getChangedTime());
$expected_label = new TranslatableMarkup('@type profile #@id', [
'@type' => $types['profile_type_0']->label(),
'@id' => $profile->id(),
]);
$this->assertEquals($expected_label, $profile->label());
// List profiles for the user and verify that the new profile appears.
$list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
@@ -146,4 +142,190 @@ class ProfileTest extends EntityKernelTestBase {
$this->assertEquals($list_ids, [$user2_profile1->id()]);
}
/**
* Tests profiles are active by default.
*/
public function testProfileActive() {
$profile_type = ProfileType::create([
'id' => 'test_defaults',
'label' => 'test_defaults',
]);
$profile_type->save();
// Create new profiles.
$profile1 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
]);
$profile1->save();
$this->assertTrue($profile1->isActive());
$profile1->setActive(FALSE);
$profile1->save();
$this->assertFalse($profile1->isActive());
}
/**
* Tests default profile functionality.
*/
public function testDefaultProfile() {
$profile_type = ProfileType::create([
'id' => 'test_defaults',
'label' => 'test_defaults',
]);
$profile_type->save();
// Create a new profile.
$profile1 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
]);
$profile1->save();
// Verify that the first profile of this type is default.
$this->assertTrue($profile1->isDefault());
// Create a second new profile.
$profile2 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
]);
$profile2->setDefault(TRUE);
$profile2->save();
$this->assertFalse($this->reloadEntity($profile1)->isDefault());
$this->assertTrue($this->reloadEntity($profile2)->isDefault());
$profile1->setDefault(TRUE)->save();
$this->assertFalse($this->reloadEntity($profile2)->isDefault());
$this->assertTrue($this->reloadEntity($profile1)->isDefault());
// Verify that a deactivated profile cannot be the default and that if the
// current default is disactivated another default is set.
$profile2->setActive(FALSE);
$profile2->save();
$this->assertFalse($this->reloadEntity($profile2)->isDefault());
$this->assertTrue($this->reloadEntity($profile1)->isDefault());
}
/**
* Tests loading default from storage handler.
*/
public function testLoadDefaultProfile() {
$profile_type = ProfileType::create([
'id' => 'test_defaults',
'label' => 'test_defaults',
]);
$profile_type->save();
// Create new profiles.
$profile1 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
]);
$profile1->setActive(TRUE);
$profile1->save();
$profile2 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
]);
$profile2->setActive(TRUE);
$profile2->setDefault(TRUE);
$profile2->save();
/** @var \Drupal\profile\ProfileStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage('profile');
$default_profile = $storage->loadDefaultByUser($this->user1, $profile_type->id());
$this->assertEquals($profile2->id(), $default_profile->id());
// Ensure that \Drupal\profile\Entity\Profile::preSave doesn't crash.
$anonymous_profile = Profile::create(['type' => $profile_type->id()]);
$anonymous_profile->save();
$this->assertTrue(empty($anonymous_profile->getOwner()));
}
/**
* Tests revisions.
*/
public function testProfileRevisions() {
$profile_type = ProfileType::create([
'id' => 'test_defaults',
'label' => 'test_defaults',
]);
$profile_type->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'profile_fullname',
'entity_type' => 'profile',
'type' => 'text',
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $profile_type->id(),
'label' => 'Full name',
]);
$field->save();
// Create new profiles.
/** @var \Drupal\profile\Entity\Profile $profile1 */
$profile1 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
'profile_fullname' => $this->randomMachineName(),
]);
$profile1->save();
$profile1 = $this->reloadEntity($profile1);
$existing_profile_id = $profile1->id();
$existing_revision_id = $profile1->getRevisionId();
$profile1->get('profile_fullname')->setValue($this->randomMachineName());
$profile1->save();
$profile1 = $this->reloadEntity($profile1);
$this->assertEquals($existing_profile_id, $profile1->id());
$this->assertEquals($existing_revision_id, $profile1->getRevisionId());
$profile_type->set('use_revisions', TRUE);
$profile_type->save();
// Create new profiles.
/** @var \Drupal\profile\Entity\Profile $profile2 */
$profile2 = Profile::create([
'type' => $profile_type->id(),
'uid' => $this->user1->id(),
'profile_fullname' => $this->randomMachineName(),
]);
$profile2->setDefault(TRUE);
$profile2->save();
$profile2 = $this->reloadEntity($profile2);
$existing_profile_id = $profile2->id();
$existing_revision_id = $profile2->getRevisionId();
// Changing field creates revision.
$profile2->get('profile_fullname')->setValue($this->randomMachineName());
$profile2->save();
$profile2 = $this->reloadEntity($profile2);
$this->assertEquals($existing_profile_id, $profile2->id());
$this->assertNotEquals($existing_revision_id, $profile2->getRevisionId());
$existing_profile_id = $profile2->id();
$existing_revision_id = $profile2->getRevisionId();
// Random save does not create a revision.
$profile2->save();
$profile2 = $this->reloadEntity($profile2);
$this->assertEquals($existing_profile_id, $profile2->id());
$this->assertEquals($existing_revision_id, $profile2->getRevisionId());
}
}
@@ -29,6 +29,9 @@ class ProfileViewTest extends ViewsKernelTestBase {
'users',
];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);