a few more base modules

This commit is contained in:
Bachir Soussi Chiadmi
2016-09-06 16:05:07 +02:00
parent c6cff46234
commit 027aa99b32
455 changed files with 43606 additions and 0 deletions
@@ -0,0 +1,12 @@
name: Profile
type: module
description: 'Test module for Profile.'
# core: 8.x
dependencies:
- profile
# Information added by Drupal.org packaging script on 2016-08-31
version: '8.x-1.0-alpha5+4-dev'
core: '8.x'
project: 'profile'
datestamp: 1472611474
@@ -0,0 +1,243 @@
langcode: en
status: true
dependencies:
module:
- profile
- user
id: users
label: Users
module: views
description: ''
tag: ''
base_table: users_field_data
base_field: uid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access user profiles'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 0
offset: 0
id: 0
total_pages: null
tags:
previous: ' Previous'
next: 'Next '
first: '« First'
last: 'Last »'
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
quantity: 9
style:
type: default
row:
type: fields
options:
default_field_elements: true
inline: { }
separator: ''
hide_empty: false
fields:
name:
id: name
table: users_field_data
field: name
entity_type: user
entity_field: name
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
plugin_id: field
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
click_sort_column: value
type: user_name
settings: { }
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
profile_id:
id: profile_id
table: profile
field: profile_id
relationship: profile
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: number_integer
settings:
thousand_separator: ''
prefix_suffix: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: profile
entity_field: profile_id
plugin_id: field
filters:
status:
value: true
table: users_field_data
field: status
plugin_id: boolean
entity_type: user
entity_field: status
id: status
expose:
operator: ''
group: 1
sorts: { }
title: Users
header: { }
footer: { }
empty: { }
relationships:
profile:
id: profile
table: users_field_data
field: profile
relationship: none
group_type: group
admin_label: Profile
required: true
entity_type: user
plugin_id: standard
arguments: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- user.permissions
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: users
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- user.permissions
tags: { }
@@ -0,0 +1,66 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AccountInterface;
/**
* Tests attaching of profile entity forms to other forms.
*
* @group profile
*/
class ProfileRegisterFormTest extends ProfileTestBase {
/**
* Test user registration integration.
*/
public function testUserRegisterForm() {
$id = $this->type->id();
$field_name = $this->field->getName();
$this->field->setRequired(TRUE);
$this->field->save();
// Allow registration without administrative approval and log in user
// directly after registering.
\Drupal::configFactory()->getEditable('user.settings')
->set('register', USER_REGISTER_VISITORS)
->set('verify_mail', 0)
->save();
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['view own test profile']);
// Verify that the additional profile field is attached and required.
$name = $this->randomMachineName();
$pass_raw = $this->randomMachineName();
$edit = [
'name' => $name,
'mail' => $this->randomMachineName() . '@example.com',
'pass[pass1]' => $pass_raw,
'pass[pass2]' => $pass_raw,
];
$this->drupalPostForm('user/register', $edit, t('Create new account'));
$this->assertSession()->pageTextContains(new FormattableMarkup('@name field is required.', ['@name' => $this->field->getLabel()]));
// Verify that we can register.
$edit["entity_" . $id . "[$field_name][0][value]"] = $this->randomMachineName();
$this->drupalPostForm(NULL, $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.');
// 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());
$this->assertEquals($profile->get($field_name)->value, $edit["entity_" . $id . "[$field_name][0][value]"], 'Field value found in loaded profile.');
// Verify that the profile field value appears on the user account page.
$this->drupalGet('user');
$this->assertSession()->pageTextContains($edit["entity_" . $id . "[$field_name][0][value]"]);
}
}
@@ -0,0 +1,140 @@
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
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;
/**
* Tests profile access handling.
*/
abstract class ProfileTestBase extends BrowserTestBase {
use ProfileTestTrait;
public static $modules = ['profile', 'field_ui', 'text', 'block'];
/**
* Testing profile type entity.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $type;
/**
* Testing profile type entity view display.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
*/
protected $display;
/**
* Testing profile type entity form display.
*
* @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form
*/
protected $form;
/**
* Testing field on profile type.
*
* @var \Drupal\Core\Field\FieldConfigInterface
*/
protected $field;
/**
* Testing admin user.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('local_tasks_block');
$this->drupalPlaceBlock('local_actions_block');
$this->drupalPlaceBlock('page_title_block');
$this->type = $this->createProfileType('test', 'Test profile', TRUE);
$id = $this->type->id();
$field_storage = FieldStorageConfig::create([
'field_name' => 'profile_fullname',
'entity_type' => 'profile',
'type' => 'text',
]);
$field_storage->save();
$this->field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->type->id(),
'label' => 'Full name',
]);
$this->field->save();
// Configure the default display.
$this->display = EntityViewDisplay::load("profile.{$this->type->id()}.default");
if (!$this->display) {
$this->display = EntityViewDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->type->id(),
'mode' => 'default',
'status' => TRUE,
]);
$this->display->save();
}
$this->display
->setComponent($this->field->getName(), ['type' => 'string'])
->save();
// Configure rhe default form.
$this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default");
if (!$this->form) {
$this->form = EntityFormDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->type->id(),
'mode' => 'default',
'status' => TRUE,
]);
$this->form->save();
}
$this->form
->setComponent($this->field->getName(), [
'type' => 'string_textfield',
])->save();
$this->checkPermissions([
'administer profile types',
"view own $id profile",
"view any $id profile",
"add own $id profile",
"add any $id profile",
"edit own $id profile",
"edit any $id profile",
"delete own $id profile",
"delete any $id profile",
]);
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access user profiles']);
$this->adminUser = $this->drupalCreateUser([
'administer profile types',
'administer profiles',
"view any $id profile",
"add any $id profile",
"edit any $id profile",
"delete any $id profile",
]);
}
}
@@ -0,0 +1,210 @@
<?php
namespace Drupal\profile\Tests;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\profile\Entity\Profile;
use Drupal\profile\ProfileTestTrait;
/**
* Tests profile access handling.
*
* @group profile
*/
class ProfileAccessTest extends EntityKernelTestBase {
use ProfileTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['user', 'system', 'field', 'text', 'profile', 'views'];
/**
* The access control handler.
*
* @var \Drupal\profile\ProfileAccessControlHandler
*/
protected $accessControlHandler;
/**
* The access manager.
*
* @var \Drupal\Core\Access\AccessManagerInterface
*/
protected $accessManager;
/**
* Testing profile type entity.
*
* @var \Drupal\profile\Entity\ProfileType
*/
protected $type;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('profile');
$this->installEntitySchema('view');
$this->installConfig(['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();
}
/**
* Tests profile create and permissions.
*/
public function testProfileCreateAccess() {
// Test user without any permissions.
$web_user1 = $this->createUser();
// Verify user does not have access to create.
$access = $this->accessControlHandler->createAccess($this->type->id(), $web_user1);
$this->assertFalse($access);
// Verify access through route.
$this->assertFalse($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
['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"]);
$web_user1->addRole($web_user2->get('roles')->first()->target_id);
$web_user1->save();
// Verify user has access to add their own profile.
$access = $this->accessControlHandler->createAccess($this->type->id(), $web_user2);
$this->assertTrue($access);
// User 2 can create own profile of type.
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
['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',
['user' => $web_user2->id(), 'profile_type' => $this->type->id()],
$web_user1
));
// Create a new profile type.
$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',
['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"]);
$this->assertTrue($this->accessControlHandler->createAccess($this->type->id(), $web_user3));
// Verify access through route.
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
['user' => $web_user3->id(), 'profile_type' => $this->type->id()],
$web_user3
));
$this->assertTrue($this->accessManager->checkNamedRoute(
'entity.profile.add_form',
['user' => $web_user2->id(), 'profile_type' => $this->type->id()],
$web_user3
));
}
/**
* Tests profile view access.
*/
public function testProfileViewAccess() {
// Setup users.
$web_user1 = $this->createUser([], [
'access user profiles',
"view own {$this->type->id()} profile",
]);
$web_user2 = $this->createUser([], [
'access user profiles',
"view any {$this->type->id()} profile",
]);
// Setup profiles.
$profile1 = Profile::create([
'uid' => $web_user1->id(),
'type' => $this->type->id(),
]);
$profile1->save();
$profile2 = Profile::create([
'uid' => $web_user2->id(),
'type' => $this->type->id(),
]);
$profile2->save();
// Test user1 can only view own profiles.
$this->assertTrue($profile1->access('view', $web_user1));
$this->assertFalse($profile2->access('view', $web_user1));
// Test user2 can view any profiles.
$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');
$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());
}
/**
* Tests profile edit flow and permissions.
*/
public function testProfileEditAccess() {
// Setup users.
$web_user1 = $this->createUser([], [
"add own {$this->type->id()} profile",
"edit own {$this->type->id()} profile",
]);
$web_user2 = $this->createUser([], [
"add any {$this->type->id()} profile",
"edit any {$this->type->id()} profile",
]);
// Setup profiles.
$profile1 = Profile::create([
'uid' => $web_user1->id(),
'type' => $this->type->id(),
]);
$profile1->save();
$profile2 = Profile::create([
'uid' => $web_user2->id(),
'type' => $this->type->id(),
]);
$profile2->save();
// Test user1 can only edit own profiles.
$this->assertTrue($profile1->access('edit', $web_user1));
$this->assertFalse($profile2->access('edit', $web_user1));
// Test user2 can edit any profiles.
$this->assertTrue($profile1->access('edit', $web_user2));
$this->assertTrue($profile2->access('edit', $web_user2));
}
}
@@ -0,0 +1,149 @@
<?php
namespace Drupal\Tests\profile\Kernel;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\profile\Entity\ProfileType;
use Drupal\profile\ProfileTestTrait;
/**
* Tests basic functionality of profiles.
*
* @group profile
*/
class ProfileTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'user',
'system',
'field',
'text',
'profile',
'views',
];
/**
* 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->installEntitySchema('profile');
$this->installEntitySchema('view');
$this->installSchema('user', ['users_data']);
$this->installConfig(['profile', 'user']);
$this->profileStorage = $this->container->get('entity_type.manager')
->getStorage('profile');
$this->user1 = $this->createUser();
$this->user2 = $this->createUser();
}
/**
* Tests the profile entity and its methods.
*/
public function testProfile() {
$types_data = [
'profile_type_0' => ['label' => $this->randomMachineName()],
'profile_type_1' => ['label' => $this->randomMachineName()],
];
/** @var \Drupal\profile\Entity\ProfileTypeInterface[] $types */
$types = [];
foreach ($types_data as $id => $values) {
$types[$id] = ProfileType::create(['id' => $id] + $values);
$types[$id]->save();
}
// Create a new profile.
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = $this->profileStorage->create($expected = [
'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);
// Save the profile.
$profile->save();
$this->assertEquals(REQUEST_TIME, $profile->getChangedTime());
// List profiles for the user and verify that the new profile appears.
$list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
$list_ids = array_keys($list);
$this->assertEquals($list_ids, [$profile->id()]);
// Create a second profile.
$user1_profile1 = $profile;
$user1_profile = $this->profileStorage->create([
'type' => $types['profile_type_0']->id(),
'uid' => $this->user1->id(),
]);
$user1_profile->save();
// List profiles for the user and verify that both profiles appear.
$list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
$list_ids = array_keys($list);
$this->assertEquals($list_ids, [$user1_profile1->id(), $user1_profile->id()]);
// Delete the second profile and verify that the first still exists.
$user1_profile->delete();
$list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
$list_ids = array_keys($list);
$this->assertEquals($list_ids, [$user1_profile1->id()]);
// Create a profile for the second user.
$user2_profile1 = $this->profileStorage->create([
'type' => $types['profile_type_0']->id(),
'uid' => $this->user2->id(),
]);
$user2_profile1->save();
// Delete the first user and verify that all of its profiles are deleted.
$this->user1->delete();
$list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
$list_ids = array_keys($list);
$this->assertEquals($list_ids, []);
// List profiles for the second user and verify that they still exist.
$list = $this->profileStorage->loadByProperties(['uid' => $this->user2->id()]);
$list_ids = array_keys($list);
$this->assertEquals($list_ids, [$user2_profile1->id()]);
}
}
@@ -0,0 +1,106 @@
<?php
namespace Drupal\Tests\profile\Kernel;
use Drupal\Component\Utility\Unicode;
use Drupal\profile\ProfileTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
/**
* Tests profile integration with Views.
*
* @group profile
*/
class ProfileViewTest extends ViewsKernelTestBase {
use ProfileTestTrait;
public static $modules = ['user', 'profile', 'profile_test'];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'users',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('user');
$this->installEntitySchema('profile');
$this->installEntitySchema('profile_type');
ViewTestData::createTestViews(get_class($this), ['profile_test']);
}
/**
* Tests views relationship with multiple referenced entities.
*
* Relationship is required, so only users with profiles will be listed.
*/
public function testProfileRelationship() {
$profile_type = $this->createProfileType();
$user[0] = $user1 = User::create([
'name' => Unicode::strtolower($this->randomMachineName()),
'status' => TRUE,
]);
$user1->save();
$user2 = User::create([
'name' => Unicode::strtolower($this->randomMachineName()),
'status' => TRUE,
]);
$user2->save();
$user[1] = $user3 = User::create([
'name' => Unicode::strtolower($this->randomMachineName()),
'status' => TRUE,
]);
$user3->save();
$user4 = User::create([
'name' => Unicode::strtolower($this->randomMachineName()),
'status' => TRUE,
]);
$user4->save();
$profile[0] = $this->createProfile($profile_type, $user1);
$profile[1] = $this->createProfile($profile_type, $user3);
Views::viewsData()->clear();
// Check table relationship exists.
$views_data = Views::viewsData()->get('users_field_data');
$this->assertEquals($views_data['profile']['relationship']['base'], 'profile');
$this->assertEquals($views_data['profile']['relationship']['base field'], 'uid');
$view = Views::getView('users');
$this->executeView($view);
// Ensure values are populated for user and profiles.
foreach ($view->result as $index => $row) {
$this->assertEquals($row->uid, $user[$index]->id(), 'User ' . $user[$index]->id() . ' found on row: ' . $index);
$this->assertEquals($row->profile_users_field_data_profile_id, $profile[$index]->id(), 'Profile ' . $profile[$index]->id() . ' found on view: ' . $index);
}
}
/**
* Create a user, and optionally a profile.
*
* @return \Drupal\user\UserInterface
* A newly created user.
*/
protected function createUser() {
$user = User::create([
'name' => Unicode::strtolower($this->randomMachineName()),
'status' => TRUE,
]);
$user->save();
return $user;
}
}