updated core and modules
This commit is contained in:
@@ -38,35 +38,22 @@ class ProfileAccessCheck implements AccessInterface {
|
||||
* The route to check against.
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The currently logged in account.
|
||||
* @param \Drupal\Core\Session\AccountInterface $user
|
||||
* The user entity.
|
||||
* @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type
|
||||
* The profile type entity.
|
||||
*
|
||||
* @return bool|\Drupal\Core\Access\AccessResultInterface
|
||||
* The access result.
|
||||
*/
|
||||
public function access(Route $route, AccountInterface $account, ProfileTypeInterface $profile_type = NULL) {
|
||||
$access_control_handler = $this->entityTypeManager->getAccessControlHandler('profile');
|
||||
|
||||
public function access(Route $route, AccountInterface $account, AccountInterface $user, ProfileTypeInterface $profile_type) {
|
||||
if ($account->hasPermission('administer profile types')) {
|
||||
return AccessResult::allowed()->cachePerPermissions();
|
||||
}
|
||||
$operation = $route->getRequirement('_profile_access_check');
|
||||
if ($operation == 'add') {
|
||||
return $access_control_handler->access($profile_type, $operation, $account, TRUE);
|
||||
}
|
||||
$own_any = ($account->id() == $user->id()) ? 'own' : 'any';
|
||||
$operation = ($profile_type->getMultiple()) ? 'view' : 'update';
|
||||
|
||||
if ($profile_type) {
|
||||
return $access_control_handler->createAccess($profile_type->id(), $account, [], TRUE);
|
||||
}
|
||||
// If checking whether a profile of any type may be created.
|
||||
foreach ($this->entityTypeManager->getStorage('profile_type')->loadMultiple() as $profile_type) {
|
||||
if (($access = $access_control_handler->createAccess($profile_type->id(), $account, [], TRUE)) && $access->isAllowed()) {
|
||||
return $access;
|
||||
}
|
||||
}
|
||||
|
||||
// No opinion.
|
||||
return AccessResult::neutral();
|
||||
return AccessResult::allowedIfHasPermission($account, "$operation $own_any {$profile_type->id()} profile");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ class ProfileController extends ControllerBase implements ContainerInjectionInte
|
||||
}
|
||||
|
||||
/**
|
||||
* The _title_callback for the entity.profile.add_form route.
|
||||
* The _title_callback for the add profile form route.
|
||||
*
|
||||
* @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type
|
||||
* The current profile type.
|
||||
@@ -106,8 +106,9 @@ class ProfileController extends ControllerBase implements ContainerInjectionInte
|
||||
/** @var \Drupal\profile\Entity\ProfileType $profile_type */
|
||||
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface|bool $active_profile */
|
||||
$active_profile = $this->entityTypeManager()->getStorage('profile')
|
||||
->loadByUser($user, $profile_type->id());
|
||||
$active_profile = $this->entityTypeManager()
|
||||
->getStorage('profile')
|
||||
->loadByUser($user, $profile_type->id());
|
||||
|
||||
// If the profile type does not support multiple, only display an add form
|
||||
// if there are no entities, or an edit for the current.
|
||||
@@ -132,8 +133,8 @@ class ProfileController extends ControllerBase implements ContainerInjectionInte
|
||||
|
||||
$build['add_profile'] = Link::createFromRoute(
|
||||
$this->t('Add new @type', ['@type' => $profile_type->label()]),
|
||||
"entity.profile.type.{$profile_type->id()}.user_profile_form.add",
|
||||
['user' => \Drupal::currentUser()->id(), 'profile_type' => $profile_type->id()])
|
||||
'entity.profile.type.user_profile_form.add',
|
||||
['user' => $this->currentUser()->id(), 'profile_type' => $profile_type->id()])
|
||||
->toRenderable();
|
||||
|
||||
// Render the active profiles.
|
||||
@@ -164,13 +165,14 @@ class ProfileController extends ControllerBase implements ContainerInjectionInte
|
||||
* A redirect back to the currency listing.
|
||||
*/
|
||||
public function setDefault(RouteMatchInterface $routeMatch) {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
|
||||
$profile = $routeMatch->getParameter('profile');
|
||||
$profile->setDefault(TRUE);
|
||||
$profile->save();
|
||||
|
||||
drupal_set_message($this->t('The %label profile has been marked as default.', ['%label' => $profile->label()]));
|
||||
|
||||
$url = $profile->urlInfo('collection');
|
||||
$url = $profile->toUrl('collection');
|
||||
return $this->redirect($url->getRouteName(), $url->getRouteParameters(), $url->getOptions());
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,11 @@ use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\Entity\ContentEntityBase;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\profile\Event\ProfileEvents;
|
||||
use Drupal\profile\Event\ProfileLabelEvent;
|
||||
use Drupal\user\UserInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Defines the profile entity class.
|
||||
*
|
||||
@@ -23,6 +25,7 @@ use Drupal\user\UserInterface;
|
||||
* "view_builder" = "Drupal\profile\ProfileViewBuilder",
|
||||
* "views_data" = "Drupal\profile\ProfileViewsData",
|
||||
* "access" = "Drupal\profile\ProfileAccessControlHandler",
|
||||
* "permission_provider" = "Drupal\profile\ProfilePermissionProvider",
|
||||
* "list_builder" = "Drupal\profile\ProfileListBuilder",
|
||||
* "form" = {
|
||||
* "default" = "Drupal\profile\Form\ProfileForm",
|
||||
@@ -31,12 +34,13 @@ use Drupal\user\UserInterface;
|
||||
* "delete" = "Drupal\profile\Form\ProfileDeleteForm",
|
||||
* },
|
||||
* "route_provider" = {
|
||||
* "html" = "Drupal\profile\ProfileHtmlRouteProvider",
|
||||
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
|
||||
* },
|
||||
* },
|
||||
* bundle_entity_type = "profile_type",
|
||||
* field_ui_base_route = "entity.profile_type.edit_form",
|
||||
* admin_permission = "administer profiles",
|
||||
* admin_permission = "administer profile",
|
||||
* permission_granularity = "bundle",
|
||||
* base_table = "profile",
|
||||
* revision_table = "profile_revision",
|
||||
* fieldable = TRUE,
|
||||
@@ -44,7 +48,6 @@ use Drupal\user\UserInterface;
|
||||
* "id" = "profile_id",
|
||||
* "revision" = "revision_id",
|
||||
* "bundle" = "type",
|
||||
* "langcode" = "langcode",
|
||||
* "uuid" = "uuid"
|
||||
* },
|
||||
* links = {
|
||||
@@ -60,78 +63,24 @@ use Drupal\user\UserInterface;
|
||||
class Profile extends ContentEntityBase implements ProfileInterface {
|
||||
|
||||
use EntityChangedTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Owner'))
|
||||
->setDescription(t('The user that owns this profile.'))
|
||||
->setRevisionable(TRUE)
|
||||
->setSetting('target_type', 'user')
|
||||
->setSetting('handler', 'default');
|
||||
|
||||
$fields['status'] = BaseFieldDefinition::create('boolean')
|
||||
->setLabel(t('Active status'))
|
||||
->setDescription(t('A boolean indicating whether the profile is active.'))
|
||||
->setDefaultValue(TRUE)
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['is_default'] = BaseFieldDefinition::create('boolean')
|
||||
->setLabel(t('Default'))
|
||||
->setDescription(t('A boolean indicating whether the profile is the default one.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['created'] = BaseFieldDefinition::create('created')
|
||||
->setLabel(t('Created'))
|
||||
->setDescription(t('The time that the profile was created.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['changed'] = BaseFieldDefinition::create('changed')
|
||||
->setLabel(t('Changed'))
|
||||
->setDescription(t('The time that the profile was last edited.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides Entity::id().
|
||||
*/
|
||||
public function id() {
|
||||
return $this->get('profile_id')->value;
|
||||
}
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function label() {
|
||||
$profile_type = ProfileType::load($this->bundle());
|
||||
return
|
||||
t('@type profile of @username (uid: @uid)',
|
||||
[
|
||||
'@type' => $profile_type->label(),
|
||||
'@username' => $this->getOwner()->getDisplayName(),
|
||||
'@uid' => $this->getOwnerId(),
|
||||
]);
|
||||
}
|
||||
$label = $this->t('@type profile #@id', [
|
||||
'@type' => $profile_type->label(),
|
||||
'@id' => $this->id(),
|
||||
]);
|
||||
// Allow the label to be overridden.
|
||||
$event = new ProfileLabelEvent($this, $label);
|
||||
$event_dispatcher = \Drupal::service('event_dispatcher');
|
||||
$event_dispatcher->dispatch(ProfileEvents::PROFILE_LABEL, $event);
|
||||
$label = $event->getLabel();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getType() {
|
||||
return $this->bundle();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setType($type) {
|
||||
$this->set('type', $this->bundle());
|
||||
return $this;
|
||||
return $label;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,6 +113,36 @@ class Profile extends ContentEntityBase implements ProfileInterface {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isActive() {
|
||||
return (bool) $this->get('status')->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setActive($active) {
|
||||
$this->set('status', (bool) $active);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isDefault() {
|
||||
return (bool) $this->get('is_default')->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefault($is_default) {
|
||||
$this->set('is_default', (bool) $is_default);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -209,36 +188,6 @@ class Profile extends ContentEntityBase implements ProfileInterface {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isActive() {
|
||||
return (bool) $this->get('status')->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setActive($active) {
|
||||
$this->set('status', $active ? PROFILE_ACTIVE : PROFILE_NOT_ACTIVE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isDefault() {
|
||||
return (bool) $this->get('is_default')->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefault($is_default) {
|
||||
$this->set('is_default', $is_default ? PROFILE_DEFAULT : PROFILE_NOT_DEFAULT);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -250,6 +199,37 @@ class Profile extends ContentEntityBase implements ProfileInterface {
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function preSave(EntityStorageInterface $storage) {
|
||||
/** @var \Drupal\profile\ProfileStorage $storage */
|
||||
parent::preSave($storage);
|
||||
|
||||
// If this profile is active and the owner has no current default profile
|
||||
// of this type, set this as the default.
|
||||
if ($this->getOwner()) {
|
||||
if ($this->isActive() && !$this->isDefault()) {
|
||||
if (!$storage->loadDefaultByUser($this->getOwner(), $this->bundle())) {
|
||||
$this->setDefault(TRUE);
|
||||
}
|
||||
}
|
||||
// Only active profiles can be default.
|
||||
elseif (!$this->isActive()) {
|
||||
$this->setDefault(FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
// Mark a new revision if the profile type supports revisions, and if there
|
||||
// has been field data changes.
|
||||
if ($this->hasTranslationChanges()) {
|
||||
/** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
|
||||
$profile_type = \Drupal::entityTypeManager()->getStorage('profile_type')->load($this->bundle());
|
||||
$this->setNewRevision($profile_type->shouldCreateNewRevision());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -258,18 +238,65 @@ class Profile extends ContentEntityBase implements ProfileInterface {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
// Check if this profile is, or became the default.
|
||||
if ($this->isDefault()) {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface[] $profiles */
|
||||
$profiles = $storage->loadMultipleByUser($this->getOwner(), $this->getType());
|
||||
if ($this->getOwner()) {
|
||||
if ($this->isDefault()) {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface[] $profiles */
|
||||
$profiles = $storage->loadMultipleByUser($this->getOwner(), $this->bundle());
|
||||
|
||||
// Ensure that all other profiles are set to not default.
|
||||
foreach ($profiles as $profile) {
|
||||
if ($profile->id() != $this->id() && $profile->isDefault()) {
|
||||
$profile->setDefault(FALSE);
|
||||
// Ensure that all other profiles are set to not default.
|
||||
foreach ($profiles as $profile) {
|
||||
if ($profile->id() != $this->id() && $profile->isDefault()) {
|
||||
$profile->setDefault(FALSE);
|
||||
$profile->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
// If this isn't the default, try to set a new one.
|
||||
elseif (!$storage->loadDefaultByUser($this->getOwner(), $this->bundle())) {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
|
||||
if ($profile = $storage->loadByUser($this->getOwner(), $this->bundle())) {
|
||||
$profile->setDefault(TRUE);
|
||||
$profile->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Owner'))
|
||||
->setDescription(t('The user that owns this profile.'))
|
||||
->setRevisionable(TRUE)
|
||||
->setSetting('target_type', 'user')
|
||||
->setSetting('handler', 'default');
|
||||
|
||||
$fields['status'] = BaseFieldDefinition::create('boolean')
|
||||
->setLabel(t('Active'))
|
||||
->setDescription(t('Whether the profile is active.'))
|
||||
->setDefaultValue(TRUE)
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['is_default'] = BaseFieldDefinition::create('boolean')
|
||||
->setLabel(t('Default'))
|
||||
->setDescription(t('Whether this is the default profile.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['created'] = BaseFieldDefinition::create('created')
|
||||
->setLabel(t('Created'))
|
||||
->setDescription(t('The time when the profile was created.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
$fields['changed'] = BaseFieldDefinition::create('changed')
|
||||
->setLabel(t('Changed'))
|
||||
->setDescription(t('The time when the profile was last edited.'))
|
||||
->setRevisionable(TRUE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,30 +7,55 @@ use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a profile entity.
|
||||
* Provides an interface for profiles.
|
||||
*/
|
||||
interface ProfileInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
|
||||
|
||||
/**
|
||||
* Returns the profile type.
|
||||
* Gets whether the profile is active.
|
||||
*
|
||||
* @return string
|
||||
* The profile type name.
|
||||
* Unpublished profiles are only visible to their authors and administrators.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the profile is active, FALSE otherwise.
|
||||
*/
|
||||
public function getType();
|
||||
public function isActive();
|
||||
|
||||
/**
|
||||
* Sets the profile type.
|
||||
* Sets whether the profile is active.
|
||||
*
|
||||
* @param string $type
|
||||
* The profile type.
|
||||
* @param bool $active
|
||||
* Whether the profile is active.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setType($type);
|
||||
public function setActive($active);
|
||||
|
||||
/**
|
||||
* Returns the profile creation timestamp.
|
||||
* Gets whether this is the user's default profile.
|
||||
*
|
||||
* A user can have a default profile of each type.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if this is the user's default profile, FALSE otherwise.
|
||||
*/
|
||||
public function isDefault();
|
||||
|
||||
/**
|
||||
* Sets whether this is the user's default profile.
|
||||
*
|
||||
* @param bool $is_default
|
||||
* Whether this is the user's default profile.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefault($is_default);
|
||||
|
||||
/**
|
||||
* Gets the profile creation timestamp.
|
||||
*
|
||||
* @return int
|
||||
* The profile creation timestamp.
|
||||
*/
|
||||
public function getCreatedTime();
|
||||
|
||||
@@ -45,7 +70,7 @@ interface ProfileInterface extends ContentEntityInterface, EntityChangedInterfac
|
||||
public function setCreatedTime($timestamp);
|
||||
|
||||
/**
|
||||
* Returns the profile revision creation timestamp.
|
||||
* Gets the profile revision creation timestamp.
|
||||
*
|
||||
* @return int
|
||||
* The UNIX timestamp of when this revision was created.
|
||||
@@ -63,7 +88,7 @@ interface ProfileInterface extends ContentEntityInterface, EntityChangedInterfac
|
||||
public function setRevisionCreationTime($timestamp);
|
||||
|
||||
/**
|
||||
* Returns the profile revision author.
|
||||
* Gets the profile revision author.
|
||||
*
|
||||
* @return \Drupal\user\UserInterface
|
||||
* The user entity for the revision author.
|
||||
@@ -80,47 +105,4 @@ interface ProfileInterface extends ContentEntityInterface, EntityChangedInterfac
|
||||
*/
|
||||
public function setRevisionAuthorId($uid);
|
||||
|
||||
/**
|
||||
* Returns a label for the profile.
|
||||
*/
|
||||
public function label();
|
||||
|
||||
/**
|
||||
* Returns the node published status indicator.
|
||||
*
|
||||
* Unpublished profiles are only visible to their authors and administrators.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the profile is active.
|
||||
*/
|
||||
public function isActive();
|
||||
|
||||
/**
|
||||
* Sets the published status of a profile.
|
||||
*
|
||||
* @param bool $active
|
||||
* TRUE to set this profile to active, FALSE to set it to inactive.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setActive($active);
|
||||
|
||||
/**
|
||||
* Returns the profile default status indicator.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the profile is default.
|
||||
*/
|
||||
public function isDefault();
|
||||
|
||||
/**
|
||||
* Sets the default status of a profile.
|
||||
*
|
||||
* @param bool $is_default
|
||||
* TRUE to set this profile to default, FALSE to set it to not default.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefault($is_default);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,8 @@ use Drupal\Core\Entity\EntityStorageInterface;
|
||||
* "roles",
|
||||
* "weight",
|
||||
* "status",
|
||||
* "langcode"
|
||||
* "langcode",
|
||||
* "use_revisions"
|
||||
* },
|
||||
* links = {
|
||||
* "add-form" = "/admin/config/people/profiles/types/add",
|
||||
@@ -54,7 +55,7 @@ class ProfileType extends ConfigEntityBundleBase implements ProfileTypeInterface
|
||||
/**
|
||||
* The primary identifier of the profile type.
|
||||
*
|
||||
* @var integer
|
||||
* @var int
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
@@ -75,14 +76,14 @@ class ProfileType extends ConfigEntityBundleBase implements ProfileTypeInterface
|
||||
/**
|
||||
* Whether the profile type is shown during registration.
|
||||
*
|
||||
* @var boolean
|
||||
* @var bool
|
||||
*/
|
||||
protected $registration = FALSE;
|
||||
|
||||
/**
|
||||
* Whether the profile type allows multiple profiles.
|
||||
*
|
||||
* @var boolean
|
||||
* @var bool
|
||||
*/
|
||||
protected $multiple = FALSE;
|
||||
|
||||
@@ -96,10 +97,17 @@ class ProfileType extends ConfigEntityBundleBase implements ProfileTypeInterface
|
||||
/**
|
||||
* The weight of the profile type compared to others.
|
||||
*
|
||||
* @var integer
|
||||
* @var int
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* Should profiles of this type always generate revisions.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $use_revisions = FALSE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -140,7 +148,7 @@ class ProfileType extends ConfigEntityBundleBase implements ProfileTypeInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setRoles($roles) {
|
||||
public function setRoles(array $roles) {
|
||||
$this->roles = $roles;
|
||||
return $this;
|
||||
}
|
||||
@@ -160,15 +168,23 @@ class ProfileType extends ConfigEntityBundleBase implements ProfileTypeInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldCreateNewRevision() {
|
||||
return $this->use_revisions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
// @todo Setting ->setRebuildNeeded isn't enough. Investigate.
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
// Rebuild module data to generate bundle permissions and link tasks.
|
||||
if (!$update) {
|
||||
system_rebuild_module_data();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
namespace Drupal\profile\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a profile type entity.
|
||||
*/
|
||||
interface ProfileTypeInterface extends ConfigEntityInterface {
|
||||
interface ProfileTypeInterface extends ConfigEntityInterface, RevisionableEntityBundleInterface {
|
||||
|
||||
/**
|
||||
* Return the registration form flag.
|
||||
@@ -50,7 +51,7 @@ interface ProfileTypeInterface extends ConfigEntityInterface {
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRoles($roles);
|
||||
public function setRoles(array $roles);
|
||||
|
||||
/**
|
||||
* Returns the profile type's weight.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Event;
|
||||
|
||||
/**
|
||||
* Defines events for the profile module.
|
||||
*/
|
||||
final class ProfileEvents {
|
||||
|
||||
/**
|
||||
* Name of the event fired when altering a profile label.
|
||||
*
|
||||
* @Event
|
||||
*
|
||||
* @see \Drupal\Profile\Event\ProfileFormatEvent
|
||||
*/
|
||||
const PROFILE_LABEL = 'profile.label';
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Event;
|
||||
|
||||
use Drupal\profile\Entity\ProfileInterface;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Defines the profile label event.
|
||||
*
|
||||
* @see \Drupal\address\Event\AddressEvents
|
||||
*/
|
||||
class ProfileLabelEvent extends Event {
|
||||
|
||||
/**
|
||||
* The profile.
|
||||
*
|
||||
* @var \Drupal\profile\Entity\ProfileInterface
|
||||
*/
|
||||
protected $profile;
|
||||
|
||||
/**
|
||||
* The label.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* Constructs a new ProfileLabelEvent object.
|
||||
*
|
||||
* @param \Drupal\profile\Entity\ProfileInterface $profile
|
||||
* The profile.
|
||||
* @param string $label
|
||||
* The profile label.
|
||||
*/
|
||||
public function __construct(ProfileInterface $profile, $label) {
|
||||
$this->profile = $profile;
|
||||
$this->label = $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the profile.
|
||||
*
|
||||
* @return \Drupal\profile\Entity\ProfileInterface
|
||||
* The profile.
|
||||
*/
|
||||
public function getProfile() {
|
||||
return $this->profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the profile label.
|
||||
*
|
||||
* @return string
|
||||
* The profile label.
|
||||
*/
|
||||
public function getLabel() {
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the profile label.
|
||||
*
|
||||
* @param string $label
|
||||
* The profile label.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel($label) {
|
||||
$this->label = $label;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -84,7 +84,7 @@ class DeleteMultiple extends ConfirmFormBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfirmText() {
|
||||
return t('Delete');
|
||||
return $this->t('Delete');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +93,7 @@ class DeleteMultiple extends ConfirmFormBase {
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$this->profiles = $this->privateTempStoreFactory->get('profile_multiple_delete_confirm')->get(\Drupal::currentUser()->id());
|
||||
if (empty($this->profiles)) {
|
||||
return new RedirectResponse(\Drupal::url('entity.profile.collection', [], ['absolute' => TRUE]));
|
||||
return new RedirectResponse(Url::fromRoute('entity.profile.collection', [], ['absolute' => TRUE]));
|
||||
}
|
||||
|
||||
$form['profiles'] = [
|
||||
|
||||
@@ -13,12 +13,36 @@ class ProfileDeleteForm extends ContentEntityDeleteForm {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
return new Url('entity.user.canonical', [
|
||||
'user' => $this->entity->getOwnerId(),
|
||||
public function getQuestion() {
|
||||
return $this->t('Are you sure you want to delete %label?', [
|
||||
'%label' => $this->getEntity()->label(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getDeletionMessage() {
|
||||
$entity = $this->getEntity();
|
||||
return $this->t('%label has been deleted.', [
|
||||
'%label' => $entity->label(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface $entity */
|
||||
$entity = $this->entity;
|
||||
if ($entity->getOwnerId()) {
|
||||
return Url::fromRoute('entity.user.canonical', [
|
||||
'user' => $entity->getOwnerId(),
|
||||
]);
|
||||
}
|
||||
return Url::fromRoute('entity.profile.collection');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -3,25 +3,14 @@
|
||||
namespace Drupal\profile\Form;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityForm;
|
||||
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\profile\Entity\ProfileType;
|
||||
|
||||
/**
|
||||
* Form controller for profile forms.
|
||||
*/
|
||||
class ProfileForm extends ContentEntityForm {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildEntity(array $form, FormStateInterface $form_state) {
|
||||
$entity = parent::buildEntity($form, $form_state);
|
||||
if ($entity->isNew()) {
|
||||
$entity->setCreatedTime(REQUEST_TIME);
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -30,12 +19,18 @@ class ProfileForm extends ContentEntityForm {
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
|
||||
$profile = $this->entity;
|
||||
|
||||
// Add an "Activate" button.
|
||||
$element['set_default'] = $element['submit'];
|
||||
$element['set_default']['#value'] = t('Save and make default');
|
||||
$element['set_default']['#weight'] = 10;
|
||||
$element['set_default']['#access'] = !$profile->isDefault();
|
||||
array_unshift($element['set_default']['#submit'], [$this, 'setDefault']);
|
||||
$profile_type_storage = $this->entityTypeManager->getStorage('profile_type');
|
||||
/** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
|
||||
$profile_type = $profile_type_storage->load($profile->bundle());
|
||||
|
||||
// Allow the profile to be saved as default if type supports multiple.
|
||||
if ($profile_type->getMultiple() && !$profile->isDefault()) {
|
||||
// Add a "make default" button.
|
||||
$element['set_default'] = $element['submit'];
|
||||
$element['set_default']['#value'] = $this->t('Save and make default');
|
||||
$element['set_default']['#weight'] = 10;
|
||||
array_unshift($element['set_default']['#submit'], [$this, 'setDefault']);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
@@ -62,28 +57,31 @@ class ProfileForm extends ContentEntityForm {
|
||||
*/
|
||||
public function deactivate(array $form, FormStateInterface $form_state) {
|
||||
$form_state->setValue('status', FALSE);
|
||||
$form_state->setValue('is_default', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$profile_type = ProfileType::load($this->entity->bundle());
|
||||
|
||||
switch ($this->entity->save()) {
|
||||
case SAVED_NEW:
|
||||
drupal_set_message($this->t('%label profile has been created.', ['%label' => $profile_type->label()]));
|
||||
drupal_set_message($this->t('%label has been created.', ['%label' => $this->entity->label()]));
|
||||
break;
|
||||
|
||||
case SAVED_UPDATED:
|
||||
drupal_set_message($this->t('%label profile has been updated.', ['%label' => $profile_type->label()]));
|
||||
drupal_set_message($this->t('%label has been updated.', ['%label' => $this->entity->label()]));
|
||||
break;
|
||||
}
|
||||
|
||||
$form_state->setRedirect('entity.user.canonical', [
|
||||
'user' => $this->entity->getOwnerId(),
|
||||
]);
|
||||
if ($this->entity->getOwnerId()) {
|
||||
$form_state->setRedirect('entity.user.canonical', [
|
||||
'user' => $this->entity->getOwnerId(),
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$form_state->setRedirect('entity.profile.collection');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class ProfileTypeDeleteForm extends EntityDeleteForm {
|
||||
->execute();
|
||||
if ($num_profiles) {
|
||||
$caption = '<p>' . \Drupal::translation()
|
||||
->formatPlural($num_profiles, '%type is used by 1 profile on your site. You can not remove this profile type until you have removed all of the %type profiles.', '%type is used by @count profiles on your site. You may not remove %type until you have removed all of the %type profiles.', ['%type' => $this->entity->label()]) . '</p>';
|
||||
->formatPlural($num_profiles, '%type is used by 1 profile on your site. You can not remove this profile type until you have removed all of the %type profiles.', '%type is used by @count profiles on your site. You may not remove %type until you have removed all of the %type profiles.', ['%type' => $this->entity->label()]) . '</p>';
|
||||
$form['#title'] = $this->entity->label();
|
||||
$form['description'] = ['#markup' => $caption];
|
||||
return $form;
|
||||
|
||||
@@ -8,7 +8,6 @@ use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\field_ui\FieldUI;
|
||||
use Drupal\user\Entity\Role;
|
||||
|
||||
|
||||
/**
|
||||
* Form controller for profile type forms.
|
||||
*/
|
||||
@@ -19,6 +18,7 @@ class ProfileTypeForm extends BundleEntityFormBase {
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::form($form, $form_state);
|
||||
/** @var \Drupal\profile\Entity\ProfileTypeInterface $type */
|
||||
$type = $this->entity;
|
||||
|
||||
if ($this->operation == 'add') {
|
||||
@@ -71,6 +71,12 @@ class ProfileTypeForm extends BundleEntityFormBase {
|
||||
}
|
||||
}
|
||||
|
||||
$form['use_revisions'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Create a new revision when a profile is modified'),
|
||||
'#default_value' => $type->shouldCreateNewRevision(),
|
||||
];
|
||||
|
||||
return $this->protectBundleIdElement($form);
|
||||
}
|
||||
|
||||
@@ -83,8 +89,8 @@ class ProfileTypeForm extends BundleEntityFormBase {
|
||||
$this->getEntity()->isNew()
|
||||
) {
|
||||
$actions['save_continue'] = $actions['submit'];
|
||||
$actions['save_continue']['#value'] = t('Save and manage fields');
|
||||
$actions['save_continue']['#submit'][] = [$this, 'redirectToFieldUI'];
|
||||
$actions['save_continue']['#value'] = $this->t('Save and manage fields');
|
||||
$actions['save_continue']['#submit'][] = [$this, 'redirectToFieldUi'];
|
||||
}
|
||||
return $actions;
|
||||
}
|
||||
@@ -97,10 +103,10 @@ class ProfileTypeForm extends BundleEntityFormBase {
|
||||
$status = $type->save();
|
||||
|
||||
if ($status == SAVED_UPDATED) {
|
||||
drupal_set_message(t('%label profile type has been updated.', ['%label' => $type->label()]));
|
||||
drupal_set_message($this->t('%label profile type has been updated.', ['%label' => $type->label()]));
|
||||
}
|
||||
else {
|
||||
drupal_set_message(t('%label profile type has been created.', ['%label' => $type->label()]));
|
||||
drupal_set_message($this->t('%label profile type has been created.', ['%label' => $type->label()]));
|
||||
}
|
||||
$form_state->setRedirect('entity.profile_type.collection');
|
||||
}
|
||||
@@ -108,7 +114,7 @@ class ProfileTypeForm extends BundleEntityFormBase {
|
||||
/**
|
||||
* Form submission handler to redirect to Manage fields page of Field UI.
|
||||
*/
|
||||
public function redirectToFieldUI(array $form, FormStateInterface $form_state) {
|
||||
public function redirectToFieldUi(array $form, FormStateInterface $form_state) {
|
||||
if ($form_state->getTriggeringElement()['#parents'][0] === 'save_continue' && $route_info = FieldUI::getOverviewRouteInfo('profile', $this->entity->id())) {
|
||||
$form_state->setRedirectUrl($route_info);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class ProfileLocalTask extends DeriverBase implements ContainerDeriverInterface
|
||||
foreach ($this->entityTypeManager->getStorage('profile_type')->loadMultiple() as $profile_type_id => $profile_type) {
|
||||
$this->derivatives["profile.type.$profile_type_id"] = [
|
||||
'title' => $profile_type->label(),
|
||||
'route_name' => "entity.profile.type.$profile_type_id.user_profile_form",
|
||||
'route_name' => 'entity.profile.type.user_profile_form',
|
||||
'base_route' => 'entity.user.canonical',
|
||||
'route_parameters' => ['profile_type' => $profile_type_id],
|
||||
'weight' => ++$weight,
|
||||
|
||||
+1
@@ -26,4 +26,5 @@ class ProfileSelection extends DefaultSelection {
|
||||
$form['target_bundles']['#title'] = $this->t('Profile types');
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class ProfileBulkForm extends BulkForm {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function emptySelectedMessage() {
|
||||
return t('No profile selected.');
|
||||
return $this->t('No profile selected.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,10 +3,8 @@
|
||||
namespace Drupal\profile;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Entity\EntityAccessControlHandler;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\entity\EntityAccessControlHandler as EntityApiAccessControlHandler;
|
||||
use Drupal\profile\Entity\ProfileType;
|
||||
|
||||
/**
|
||||
@@ -14,99 +12,23 @@ use Drupal\profile\Entity\ProfileType;
|
||||
*
|
||||
* @see \Drupal\profile\Entity\Profile
|
||||
*/
|
||||
class ProfileAccessControlHandler extends EntityAccessControlHandler {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = [], $return_as_object = FALSE) {
|
||||
$account = $this->prepareUser($account);
|
||||
|
||||
if ($account->hasPermission('bypass profile access')) {
|
||||
$result = AccessResult::allowed()->cachePerPermissions();
|
||||
return $return_as_object ? $result : $result->isAllowed();
|
||||
}
|
||||
|
||||
$result = parent::createAccess($entity_bundle, $account, $context, TRUE)->cachePerPermissions();
|
||||
return $return_as_object ? $result : $result->isAllowed();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* When the $operation is 'add' then the $entity is of type 'profile_type',
|
||||
* otherwise $entity is of type 'profile'.
|
||||
*/
|
||||
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
|
||||
$account = $this->prepareUser($account);
|
||||
|
||||
$user_page = \Drupal::request()->attributes->get('user');
|
||||
|
||||
// Some times, operation edit is called update.
|
||||
// Use edit in any case.
|
||||
if ($operation == 'update') {
|
||||
$operation = 'edit';
|
||||
}
|
||||
|
||||
// Check that if profile type has require roles, the user the profile is
|
||||
// being added to has any of the required roles.
|
||||
if ($entity->getEntityTypeId() == 'profile') {
|
||||
$profile_roles = ProfileType::load($entity->bundle())->getRoles();
|
||||
// Retrieve all user roles including locked roles.
|
||||
$user_roles = $entity->getOwner()->getRoles();
|
||||
if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
}
|
||||
elseif ($entity->getEntityTypeId() == 'profile_type') {
|
||||
$profile_roles = $entity->getRoles();
|
||||
// Retrieve all user roles including locked roles.
|
||||
$user_roles = User::load($user_page->id())->getRoles();
|
||||
if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
if ($account->hasPermission('bypass profile access')) {
|
||||
return AccessResult::allowed()->cachePerPermissions();
|
||||
}
|
||||
elseif (
|
||||
(
|
||||
$operation == 'add'
|
||||
&& (
|
||||
(
|
||||
$user_page->id() == $account->id()
|
||||
&& $account->hasPermission($operation . ' own ' . $entity->id() . ' profile')
|
||||
)
|
||||
|| $account->hasPermission($operation . ' any ' . $entity->id() . ' profile')
|
||||
)
|
||||
) || (
|
||||
$operation != 'add'
|
||||
&& (
|
||||
(
|
||||
$entity->getOwnerId() == $account->id()
|
||||
&& $account->hasPermission($operation . ' own ' . $entity->getType() . ' profile')
|
||||
)
|
||||
|| $account->hasPermission($operation . ' any ' . $entity->getType() . ' profile')
|
||||
)
|
||||
)
|
||||
){
|
||||
return AccessResult::allowed()->cachePerPermissions();
|
||||
}
|
||||
else {
|
||||
// No opinion.
|
||||
return AccessResult::neutral()->cachePerPermissions();
|
||||
}
|
||||
}
|
||||
class ProfileAccessControlHandler extends EntityApiAccessControlHandler {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
|
||||
return AccessResult::allowedIfHasPermissions($account, [
|
||||
'add any ' . $entity_bundle . ' profile',
|
||||
'add own ' . $entity_bundle . ' profile',
|
||||
], 'OR');
|
||||
$result = parent::checkCreateAccess($account, $context, $entity_bundle);
|
||||
|
||||
// If access is allowed, check role restriction.
|
||||
if ($result->isAllowed()) {
|
||||
$bundle = ProfileType::load($entity_bundle);
|
||||
if (!empty(array_filter($bundle->getRoles()))) {
|
||||
$result = AccessResult::allowedIf(!empty(array_intersect($account->getRoles(), $bundle->getRoles())));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
|
||||
use Drupal\profile\Entity\ProfileType;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
/**
|
||||
* Provides HTML routes for the profile entity type.
|
||||
*/
|
||||
class ProfileHtmlRouteProvider extends DefaultHtmlRouteProvider {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRoutes(EntityTypeInterface $entity_type) {
|
||||
$collection = parent::getRoutes($entity_type);
|
||||
|
||||
/** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
|
||||
foreach (ProfileType::loadMultiple() as $profile_type) {
|
||||
$route = (new Route(
|
||||
"/user/{user}/{profile_type}",
|
||||
['_controller' => '\Drupal\profile\Controller\ProfileController::userProfileForm',
|
||||
'_title_callback' => '\Drupal\profile\Controller\ProfileController::addPageTitle'
|
||||
],
|
||||
['_profile_access_check' => 'add'],
|
||||
[
|
||||
'parameters' => [
|
||||
'user' => ['type' => 'entity:user'],
|
||||
'profile_type' => ['type' => 'entity:profile_type'],
|
||||
],
|
||||
])
|
||||
);
|
||||
$collection->add("entity.profile.type.{$profile_type->id()}.user_profile_form", $route);
|
||||
|
||||
// If the profile type supports multiple, we need an additional route for
|
||||
// adding new profiles.
|
||||
if ($profile_type->getMultiple()) {
|
||||
$route = (new Route(
|
||||
"/user/{user}/{profile_type}/add",
|
||||
['_controller' => '\Drupal\profile\Controller\ProfileController::addProfile'],
|
||||
['_profile_access_check' => 'add'],
|
||||
[
|
||||
'parameters' => [
|
||||
'user' => ['type' => 'entity:user'],
|
||||
'profile_type' => ['type' => 'entity:profile_type'],
|
||||
],
|
||||
])
|
||||
);
|
||||
$collection->add("entity.profile.type.{$profile_type->id()}.user_profile_form.add", $route);
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class ProfileListBuilder extends EntityListBuilder {
|
||||
$options += ($langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : []);
|
||||
$uri->setOptions($options);
|
||||
$row['label'] = $entity->toLink();
|
||||
$row['type'] = $entity->getType();
|
||||
$row['type'] = $entity->bundle();
|
||||
$row['owner']['data'] = [
|
||||
'#theme' => 'username',
|
||||
'#account' => $entity->getOwner(),
|
||||
@@ -126,7 +126,7 @@ class ProfileListBuilder extends EntityListBuilder {
|
||||
public function getOperations(EntityInterface $entity) {
|
||||
$operations = parent::getOperations($entity);
|
||||
|
||||
if (!$entity->isDefault()) {
|
||||
if ($entity->isActive() && !$entity->isDefault()) {
|
||||
$operations['set_default'] = [
|
||||
'title' => $this->t('Mark as default'),
|
||||
'url' => $entity->toUrl('set-default'),
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\entity\EntityPermissionProvider;
|
||||
|
||||
/**
|
||||
* Providers Profile entity permissions.
|
||||
*
|
||||
* Extends the Entity API permission provider to support bundle based view
|
||||
* permissions.
|
||||
*/
|
||||
class ProfilePermissionProvider extends EntityPermissionProvider {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
|
||||
$permissions = parent::buildBundlePermissions($entity_type);
|
||||
$singular_label = $entity_type->getSingularLabel();
|
||||
$entity_type_id = $entity_type->id();
|
||||
$bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
|
||||
|
||||
foreach ($bundles as $bundle_name => $bundle_info) {
|
||||
$permissions["view any {$bundle_name} {$entity_type_id}"] = [
|
||||
'title' => $this->t('@bundle: View any @type', [
|
||||
'@bundle' => $bundle_info['label'],
|
||||
'@type' => $singular_label,
|
||||
]),
|
||||
];
|
||||
$permissions["view own {$bundle_name} {$entity_type_id}"] = [
|
||||
'title' => $this->t('@bundle: View own @type', [
|
||||
'@bundle' => $bundle_info['label'],
|
||||
'@type' => $singular_label,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\profile\Entity\ProfileType;
|
||||
|
||||
/**
|
||||
* Defines a class containing permission callbacks.
|
||||
*/
|
||||
class ProfilePermissions {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Returns an array of profile type permissions.
|
||||
*
|
||||
* @return array
|
||||
* Returns an array of permissions.
|
||||
*/
|
||||
public function profileTypePermissions() {
|
||||
$perms = [];
|
||||
// Generate profile permissions for all profile types.
|
||||
foreach (ProfileType::loadMultiple() as $type) {
|
||||
$perms += $this->buildPermissions($type);
|
||||
}
|
||||
|
||||
return $perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a standard list of permissions for a given profile type.
|
||||
*
|
||||
* @param \Drupal\profile\Entity\ProfileType $profile_type
|
||||
* The machine name of the profile type.
|
||||
*
|
||||
* @return array
|
||||
* An array of permission names and descriptions.
|
||||
*/
|
||||
protected function buildPermissions(ProfileType $profile_type) {
|
||||
$type_id = $profile_type->id();
|
||||
$type_params = ['%type' => $profile_type->label()];
|
||||
|
||||
return [
|
||||
"add own $type_id profile" => [
|
||||
'title' => $this->t('%type: Add own profile', $type_params),
|
||||
],
|
||||
"add any $type_id profile" => [
|
||||
'title' => $this->t('%type: Add any profile', $type_params),
|
||||
],
|
||||
"view own $type_id profile" => [
|
||||
'title' => $this->t('%type: View own profile', $type_params),
|
||||
],
|
||||
"view any $type_id profile" => [
|
||||
'title' => $this->t('%type: View any profile', $type_params),
|
||||
],
|
||||
"edit own $type_id profile" => [
|
||||
'title' => $this->t('%type: Edit own profile', $type_params),
|
||||
],
|
||||
"edit any $type_id profile" => [
|
||||
'title' => $this->t('%type: Edit any profile', $type_params),
|
||||
],
|
||||
"delete own $type_id profile" => [
|
||||
'title' => $this->t('%type: Delete own profile', $type_params),
|
||||
],
|
||||
"delete any $type_id profile" => [
|
||||
'title' => $this->t('%type: Delete any profile', $type_params),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class ProfileStorage extends SqlContentEntityStorage implements ProfileStorageIn
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadByUser(AccountInterface $account, $profile_type, $active = PROFILE_ACTIVE) {
|
||||
public function loadByUser(AccountInterface $account, $profile_type, $active = TRUE) {
|
||||
$result = $this->loadByProperties([
|
||||
'uid' => $account->id(),
|
||||
'type' => $profile_type,
|
||||
@@ -26,12 +26,12 @@ class ProfileStorage extends SqlContentEntityStorage implements ProfileStorageIn
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadMultipleByUser(AccountInterface $account, $profile_type, $active = PROFILE_ACTIVE) {
|
||||
public function loadMultipleByUser(AccountInterface $account, $profile_type, $active = TRUE) {
|
||||
return $this->loadByProperties([
|
||||
'uid' => $account->id(),
|
||||
'type' => $profile_type,
|
||||
'status' => $active,
|
||||
]);
|
||||
'uid' => $account->id(),
|
||||
'type' => $profile_type,
|
||||
'status' => $active,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,7 +41,7 @@ class ProfileStorage extends SqlContentEntityStorage implements ProfileStorageIn
|
||||
$result = $this->loadByProperties([
|
||||
'uid' => $account->id(),
|
||||
'type' => $profile_type,
|
||||
'status' => PROFILE_ACTIVE,
|
||||
'status' => TRUE,
|
||||
'is_default' => TRUE,
|
||||
]);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ trait ProfileTestTrait {
|
||||
* The profile type machine name.
|
||||
* @param string $label
|
||||
* The profile type human display name.
|
||||
* @param bool|FALSE $registration
|
||||
* @param bool $registration
|
||||
* Boolean if profile type shows on registration form.
|
||||
* @param array $roles
|
||||
* Array of user role machine names.
|
||||
@@ -29,7 +29,7 @@ trait ProfileTestTrait {
|
||||
* @return \Drupal\profile\Entity\ProfileTypeInterface
|
||||
* Returns a profile type entity.
|
||||
*/
|
||||
protected function createProfileType($id = NULL, $label = NULL, $registration = FALSE, $roles = []) {
|
||||
protected function createProfileType($id = NULL, $label = NULL, $registration = FALSE, array $roles = []) {
|
||||
$id = !empty($id) ? $id : $this->randomMachineName();
|
||||
$label = !empty($label) ? $label : $this->randomMachineName();
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ class ProfileTypeListBuilder extends ConfigEntityListBuilder {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildHeader() {
|
||||
$header['type'] = t('Profile type');
|
||||
$header['registration'] = t('Registration');
|
||||
$header['multiple'] = t('Allow multiple profiles');
|
||||
$header['type'] = $this->t('Profile type');
|
||||
$header['registration'] = $this->t('Registration');
|
||||
$header['multiple'] = $this->t('Allow multiple profiles');
|
||||
return $header + parent::buildHeader();
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ class ProfileTypeListBuilder extends ConfigEntityListBuilder {
|
||||
*/
|
||||
public function buildRow(EntityInterface $entity) {
|
||||
$row['type'] = $entity->toLink(NULL, 'edit-form');
|
||||
$row['registration'] = $entity->getRegistration() ? t('Yes') : t('No');
|
||||
$row['multiple'] = $entity->getMultiple() ? t('Yes') : t('No');
|
||||
$row['registration'] = $entity->getRegistration() ? $this->t('Yes') : $this->t('No');
|
||||
$row['multiple'] = $entity->getMultiple() ? $this->t('Yes') : $this->t('No');
|
||||
return $row + parent::buildRow($entity);
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ class ProfileTypeListBuilder extends ConfigEntityListBuilder {
|
||||
// which have the weights 15, 20, 25.
|
||||
if (isset($operations['edit'])) {
|
||||
$operations['edit'] = [
|
||||
'title' => t('Edit'),
|
||||
'title' => $this->t('Edit'),
|
||||
'weight' => 30,
|
||||
'url' => $entity->toUrl('edit-form'),
|
||||
];
|
||||
}
|
||||
if (isset($operations['delete'])) {
|
||||
$operations['delete'] = [
|
||||
'title' => t('Delete'),
|
||||
'title' => $this->t('Delete'),
|
||||
'weight' => 35,
|
||||
'url' => $entity->toUrl('delete-form'),
|
||||
];
|
||||
|
||||
@@ -14,10 +14,10 @@ class ProfileViewsData extends EntityViewsData {
|
||||
*/
|
||||
public function getViewsData() {
|
||||
$data = parent::getViewsData();
|
||||
|
||||
$data['profile']['type']['title'] = t('Profile type');
|
||||
$data['profile']['profile_bulk_form'] = [
|
||||
'title' => t('Profile operations bulk form'),
|
||||
'help' => t('Add a form element that lets you run operations on multiple profiles.'),
|
||||
'title' => $this->t('Profile operations bulk form'),
|
||||
'help' => $this->t('Add a form element that lets you run operations on multiple profiles.'),
|
||||
'field' => [
|
||||
'id' => 'profile_bulk_form',
|
||||
],
|
||||
|
||||
@@ -1,411 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
use Drupal\profile\Entity\Profile;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests basic CRUD functionality of profiles.
|
||||
*
|
||||
* @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 profiles',
|
||||
'administer profile types',
|
||||
'bypass profile access',
|
||||
'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 profiles are active by default.
|
||||
*/
|
||||
public function testProfileActive() {
|
||||
$profile_type = $this->createProfileType('test_defaults', 'test_defaults');
|
||||
|
||||
// Create new profiles.
|
||||
$profile1 = Profile::create($expected = [
|
||||
'type' => $profile_type->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile1->save();
|
||||
|
||||
$this->assertTrue($profile1->isActive());
|
||||
|
||||
$profile1->setActive(PROFILE_NOT_ACTIVE);
|
||||
$profile1->save();
|
||||
|
||||
$this->assertFalse($profile1->isActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests default profile functionality.
|
||||
*/
|
||||
public function testDefaultProfile() {
|
||||
$profile_type = $this->createProfileType('test_defaults', 'test_defaults');
|
||||
|
||||
// Create new profiles.
|
||||
$profile1 = Profile::create($expected = [
|
||||
'type' => $profile_type->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile1->save();
|
||||
$profile2 = Profile::create($expected = [
|
||||
'type' => $profile_type->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile2->setDefault(TRUE);
|
||||
$profile2->save();
|
||||
|
||||
$this->assertFalse($profile1->isDefault());
|
||||
$this->assertTrue($profile2->isDefault());
|
||||
|
||||
$profile1->setDefault(TRUE)->save();
|
||||
|
||||
$this->assertFalse(Profile::load($profile2->id())->isDefault());
|
||||
$this->assertTrue(Profile::load($profile1->id())->isDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests loading default from storage handler.
|
||||
*/
|
||||
public function testLoadDefaultProfile() {
|
||||
$profile_type = $this->createProfileType('test_defaults', 'test_defaults');
|
||||
|
||||
// Create new profiles.
|
||||
$profile1 = Profile::create($expected = [
|
||||
'type' => $profile_type->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile1->setActive(TRUE);
|
||||
$profile1->save();
|
||||
$profile2 = Profile::create($expected = [
|
||||
'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->assertEqual($profile2->id(), $default_profile->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests mark as default action.
|
||||
*/
|
||||
public function testDefaultAction() {
|
||||
$types_data = [
|
||||
'profile_type_0' => [
|
||||
'label' => $this->randomMachineName(),
|
||||
'multiple' => TRUE,
|
||||
],
|
||||
'profile_type_1' => [
|
||||
'label' => $this->randomMachineName(),
|
||||
'multiple' => TRUE,
|
||||
],
|
||||
];
|
||||
|
||||
/** @var ProfileType[] $types */
|
||||
$types = [];
|
||||
foreach ($types_data as $id => $values) {
|
||||
$types[$id] = $this->createProfileType($id, $values['label']);
|
||||
}
|
||||
|
||||
$restricted_user = $this->drupalCreateUser([
|
||||
'administer profiles',
|
||||
'edit own ' . $types['profile_type_0']->id() . ' profile',
|
||||
'edit own ' . $types['profile_type_1']->id() . ' profile',
|
||||
]);
|
||||
|
||||
$admin_user = $this->drupalCreateUser([
|
||||
'administer profiles',
|
||||
'edit any ' . $types['profile_type_0']->id() . ' profile',
|
||||
'edit any ' . $types['profile_type_1']->id() . ' profile',
|
||||
]);
|
||||
|
||||
// Create new profiles.
|
||||
$profile_profile_type_0_restricted_user = Profile::create($expected = [
|
||||
'type' => $types['profile_type_0']->id(),
|
||||
'uid' => $restricted_user->id(),
|
||||
]);
|
||||
$profile_profile_type_0_restricted_user->setActive(TRUE);
|
||||
$profile_profile_type_0_restricted_user->save();
|
||||
$profile_profile_type_0_user1_1 = Profile::create($expected = [
|
||||
'type' => $types['profile_type_0']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile_profile_type_0_user1_1->setActive(TRUE);
|
||||
$profile_profile_type_0_user1_1->save();
|
||||
$profile_profile_type_0_user1_2 = Profile::create($expected = [
|
||||
'type' => $types['profile_type_0']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile_profile_type_0_user1_2->setActive(TRUE);
|
||||
$profile_profile_type_0_user1_2->save();
|
||||
$profile_profile_type_1_user1 = Profile::create($expected = [
|
||||
'type' => $types['profile_type_1']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile_profile_type_1_user1->setActive(TRUE);
|
||||
$profile_profile_type_1_user1->save();
|
||||
$profile_profile_type_1_user2 = Profile::create($expected = [
|
||||
'type' => $types['profile_type_1']->id(),
|
||||
'uid' => $this->user2->id(),
|
||||
]);
|
||||
$profile_profile_type_1_user2->setActive(TRUE);
|
||||
$profile_profile_type_1_user2->save();
|
||||
$profile_profile_type_1_user1_inactive = Profile::create($expected = [
|
||||
'type' => $types['profile_type_1']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile_profile_type_1_user1_inactive->setActive(FALSE);
|
||||
$profile_profile_type_1_user1_inactive->save();
|
||||
$profile_profile_type_1_user1_active = Profile::create($expected = [
|
||||
'type' => $types['profile_type_1']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile_profile_type_1_user1_active->isActive(TRUE);
|
||||
$profile_profile_type_1_user1_active->save();
|
||||
|
||||
// Make sure that $restricted_user is allowed to set default his own profile
|
||||
// and not others'.
|
||||
$this->drupalLogin($restricted_user);
|
||||
|
||||
$this->drupalGet('admin/config/people/profiles');
|
||||
|
||||
$this->clickLink('Mark as default', 0);
|
||||
$this->assertTrue(Profile::load($profile_profile_type_0_restricted_user->id())->isDefault());
|
||||
$this->clickLink('Mark as default', 1);
|
||||
$this->assertResponse(403);
|
||||
$this->drupalLogout();
|
||||
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
$this->drupalGet('admin/config/people/profiles');
|
||||
|
||||
// Mark $profile_profile_type_0_user1_1 as default
|
||||
// $profile_profile_type_0_user1_2 should stay not default.
|
||||
$this->clickLink('Mark as default', 0);
|
||||
$this->assertTrue(Profile::load($profile_profile_type_0_user1_1->id())->isDefault());
|
||||
$this->assertFalse($profile_profile_type_0_user1_2->isDefault());
|
||||
|
||||
// Mark $profile_profile_type_0_user1_2 as default
|
||||
// $profile_profile_type_0_user1_1 should become not default.
|
||||
$profile_profile_type_0_user1_2->setDefault(TRUE);
|
||||
$profile_profile_type_0_user1_2->save();
|
||||
$this->assertTrue($profile_profile_type_0_user1_2->isDefault());
|
||||
$this->assertFalse($profile_profile_type_0_user1_1->isDefault());
|
||||
|
||||
// Mark $profile_profile_type_1_user1 as default
|
||||
// $profile_profile_type_1_user2 should stay not default.
|
||||
$this->clickLink('Mark as default', 1);
|
||||
$this->assertTrue(Profile::load($profile_profile_type_1_user1->id())->isDefault());
|
||||
$this->assertFalse($profile_profile_type_1_user2->isDefault());
|
||||
|
||||
// Mark $profile_profile_type_1_user2 as default
|
||||
// $profile_profile_type_1_user1 should stay default.
|
||||
$profile_profile_type_1_user2->setDefault(TRUE);
|
||||
$profile_profile_type_1_user2->save();
|
||||
$this->assertTrue($profile_profile_type_1_user2->isDefault());
|
||||
$this->assertTrue(Profile::load($profile_profile_type_1_user1->id())->isDefault());
|
||||
|
||||
// Mark $profile_profile_type_1_user1_inactive as default
|
||||
// $profile_profile_type_1_user1_active should stay not default.
|
||||
$this->clickLink('Mark as default', 2);
|
||||
$this->assertTrue(Profile::load($profile_profile_type_1_user1_inactive->id())->isDefault());
|
||||
$this->assertFalse($profile_profile_type_1_user1_active->isDefault());
|
||||
|
||||
// Mark $profile_profile_type_1_user1_active as default
|
||||
// $profile_profile_type_1_user1_inactive should stay default.
|
||||
$profile_profile_type_1_user1_active->setDefault(TRUE);
|
||||
$profile_profile_type_1_user1_active->save();
|
||||
$this->assertTrue($profile_profile_type_1_user1_active->isDefault());
|
||||
$this->assertTrue(Profile::load($profile_profile_type_1_user1_inactive->id())->isDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether profile default on edit is working.
|
||||
*/
|
||||
public function testProfileEdit() {
|
||||
$types_data = [
|
||||
'profile_type_0' => [
|
||||
'label' => $this->randomMachineName(),
|
||||
'multiple' => TRUE,
|
||||
],
|
||||
];
|
||||
|
||||
/** @var \Drupal\profile\Entity\ProfileTypeInterface[] $types */
|
||||
$types = [];
|
||||
foreach ($types_data as $id => $values) {
|
||||
$types[$id] = $this->createProfileType($id, $values['label']);
|
||||
}
|
||||
|
||||
$admin_user = $this->drupalCreateUser([
|
||||
'administer profiles',
|
||||
'administer users',
|
||||
'edit any ' . $types['profile_type_0']->id() . ' profile',
|
||||
]);
|
||||
|
||||
// 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_0']->id(),
|
||||
'uid' => $this->user1->id(),
|
||||
]);
|
||||
$profile2->setDefault(TRUE);
|
||||
$profile2->save();
|
||||
|
||||
$this->assertFalse($profile1->isDefault());
|
||||
$this->assertTrue($profile2->isDefault());
|
||||
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
$this->drupalPostForm("profile/{$profile1->id()}/edit", [], 'Save and make default');
|
||||
|
||||
\Drupal::entityTypeManager()->getStorage('profile')->resetCache([$profile1->id(), $profile2->id()]);
|
||||
$this->assertTrue(Profile::load($profile1->id())->isDefault());
|
||||
$this->assertFalse(Profile::load($profile2->id())->isDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests administrative-only profiles.
|
||||
*/
|
||||
public function testAdminOnlyProfiles() {
|
||||
$id = $this->type->id();
|
||||
$field_name = $this->field->getName();
|
||||
|
||||
// Create a test user account.
|
||||
$web_user = $this->drupalCreateUser(['access user profiles']);
|
||||
$uid = $web_user->id();
|
||||
$value = $this->randomMachineName();
|
||||
|
||||
// Administratively enter profile field values for the new account.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$edit = [
|
||||
"{$this->field->getName()}[0][value]" => $value,
|
||||
];
|
||||
$this->drupalPostForm("user/$uid/$id", $edit, 'Save and make default');
|
||||
|
||||
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
|
||||
$profile = \Drupal::entityTypeManager()
|
||||
->getStorage('profile')
|
||||
->loadByUser($web_user, $this->type->id());
|
||||
$profile_id = $profile->id();
|
||||
|
||||
$this->assertEqual($profile->getType(), $this->type->id());
|
||||
|
||||
/*
|
||||
// Verify that the administrator can see the profile.
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertText($this->type->label());
|
||||
$this->assertText($value);
|
||||
$this->drupalLogout();
|
||||
// Verify that the user can not access, create or edit the profile.
|
||||
$this->drupalLogin($web_user);
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertNoText($this->type->label());
|
||||
$this->assertNoText($value);
|
||||
$this->drupalGet("user/$uid/edit/profile/$id/$profile_id");
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Check edit link isn't displayed.
|
||||
$this->assertNoLinkByHref("user/$uid/edit/profile/$id/$profile_id");
|
||||
// Check delete link isn't displayed.
|
||||
$this->assertNoLinkByHref("user/$uid/delete/profile/$id/$profile_id");
|
||||
|
||||
|
||||
// Allow users to edit own profiles.
|
||||
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ["edit own $id profile"]);
|
||||
|
||||
// Verify that the user is able to edit the own profile.
|
||||
$value = $this->randomMachineName();
|
||||
$edit = [
|
||||
"{$field_name}[0][value]" => $value,
|
||||
];
|
||||
$this->drupalPostForm("user/$uid/edit/profile/$id/$profile_id", $edit, t('Save'));
|
||||
$this->assertText(new FormattableMarkup('profile has been updated.', []));
|
||||
|
||||
|
||||
// Verify that the own profile is still not visible on the account page.
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertNoText($this->type->label());
|
||||
$this->assertNoText($value);
|
||||
|
||||
// Allow users to view own profiles.
|
||||
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ["view own $id profile"]);
|
||||
|
||||
// Verify that the own profile is visible on the account page.
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertText($this->type->label());
|
||||
$this->assertText($value);
|
||||
|
||||
// Allow users to delete own profiles.
|
||||
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ["delete own $id profile"]);
|
||||
|
||||
// Verify that the user can delete the own profile.
|
||||
$this->drupalGet("user/$uid/edit/profile/$id/$profile_id");
|
||||
$this->clickLink(t('Delete'));
|
||||
$this->drupalPostForm(NULL, [], t('Delete'));
|
||||
$this->assertRaw(new FormattableMarkup('@label profile deleted.', ['@label' => $this->type->label()]));
|
||||
$this->assertUrl("user/$uid");
|
||||
|
||||
// Verify that the profile is gone.
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertNoText($this->type->label());
|
||||
$this->assertNoText($value);
|
||||
$this->drupalGet("user/$uid/edit/profile/$id");
|
||||
$this->assertNoText($value);
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Tests profile field access functionality.
|
||||
*
|
||||
* @group profile
|
||||
*/
|
||||
class ProfileFieldAccessTest extends ProfileTestBase {
|
||||
|
||||
private $webUser;
|
||||
private $otherUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->adminUser = $this->drupalCreateUser([
|
||||
'access user profiles',
|
||||
'administer profile types',
|
||||
'administer profile fields',
|
||||
'administer profile display',
|
||||
'bypass profile access',
|
||||
]);
|
||||
|
||||
$user_permissions = [
|
||||
'access user profiles',
|
||||
"add own {$this->type->id()} profile",
|
||||
"edit own {$this->type->id()} profile",
|
||||
"view own {$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->drupalLogin($this->adminUser);
|
||||
|
||||
// Create a private profile field.
|
||||
$edit = [
|
||||
'new_storage_type' => 'string',
|
||||
'label' => 'Secret',
|
||||
'field_name' => 'secret',
|
||||
];
|
||||
$this->drupalPostForm("admin/config/people/profiles/types/manage/{$this->type->id()}/fields/add-field", $edit, t('Save and continue'));
|
||||
$this->drupalPostForm(NULL, [], t('Save field settings'));
|
||||
$edit = [
|
||||
'profile_private' => 1,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save settings'));
|
||||
|
||||
// Fill in a field value.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$uid = $this->webUser->id();
|
||||
$secret = $this->randomMachineName();
|
||||
$edit = [
|
||||
'field_secret[0][value]' => $secret,
|
||||
];
|
||||
$this->drupalPostForm("user/$uid/{$this->type->id()}", $edit, t('Save'));
|
||||
|
||||
// User cache page need to be cleared to see new profile.
|
||||
Cache::invalidateTags([
|
||||
'user:' . $uid,
|
||||
'user_view',
|
||||
]);
|
||||
|
||||
// Verify that the private field value appears for the profile owner.
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertText($secret);
|
||||
|
||||
// Verify that the private field value appears for the administrator.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertText($secret);
|
||||
|
||||
// Verify that the private field value does not appear for other users.
|
||||
$this->drupalLogin($this->otherUser);
|
||||
$this->drupalGet("user/$uid");
|
||||
$this->assertNoText($secret);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
|
||||
/**
|
||||
* Tests profile role access handling.
|
||||
*
|
||||
* @group profile
|
||||
*/
|
||||
class ProfileRoleAccessTest extends ProfileTestBase {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->role1 = $this->drupalCreateRole([]);
|
||||
$this->role2 = $this->drupalCreateRole([]);
|
||||
$this->type2 = $this->createProfileType(NULL, NULL, FALSE, [$this->role2]);
|
||||
$this->type3 = $this->createProfileType(NULL, NULL, FALSE, [$this->role1, $this->role2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests add profile form access for a profile type that does not require
|
||||
* users to have a role.
|
||||
*/
|
||||
public function testProfileWithNoRoles() {
|
||||
// Create user with add own profile permissions.
|
||||
$web_user1 = $this->drupalCreateUser(["add own {$this->type->id()} profile"]);
|
||||
$this->drupalLogin($web_user1);
|
||||
|
||||
// Test user without role can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type->id()}");
|
||||
$this->assertResponse(200);
|
||||
}
|
||||
|
||||
public function testLockedRoles() {
|
||||
$locked_role_type = $this->createProfileType(NULL, NULL, FALSE, [AccountInterface::AUTHENTICATED_ROLE]);
|
||||
|
||||
// Create user with add own profile permissions.
|
||||
$web_user1 = $this->drupalCreateUser(["add own {$locked_role_type->id()} profile"]);
|
||||
$this->drupalLogin($web_user1);
|
||||
|
||||
// Test user without role can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$locked_role_type->id()}");
|
||||
$this->assertResponse(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests add profile form access for a profile type that requires users to
|
||||
* have a single role.
|
||||
*/
|
||||
public function testProfileWithSingleRole() {
|
||||
// Create user with add own profile permissions.
|
||||
$web_user1 = $this->drupalCreateUser(["add own {$this->type2->id()} profile"]);
|
||||
$this->drupalLogin($web_user1);
|
||||
|
||||
// Test user without role can access add profile form.
|
||||
// Expected: User cannot access form.
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type2->id()}");
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Test user with wrong role can access add profile form.
|
||||
// Expected: User cannot access form.
|
||||
$web_user1->addRole($this->role1);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type2->id()}");
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Test user with correct role can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$web_user1->removeRole($this->role1);
|
||||
$web_user1->addRole($this->role2);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type2->id()}");
|
||||
$this->assertResponse(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests add profile form access for a profile type that requires users to
|
||||
* have one of multiple roles.
|
||||
*/
|
||||
public function testProfileWithAllRoles() {
|
||||
// Create user with add own profile permissions.
|
||||
$web_user1 = $this->drupalCreateUser(["add own {$this->type3->id()} profile"]);
|
||||
$this->drupalLogin($web_user1);
|
||||
|
||||
// Test user without role can access add profile form.
|
||||
// Expected: User cannot access form.
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}");
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Test user with role 1 can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$web_user1->addRole($this->role1);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}");
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Test user with both roles can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$web_user1->addRole($this->role2);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}");
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Test user with role 2 can access add profile form.
|
||||
// Expected: User can access form.
|
||||
$web_user1->removeRole($this->role1);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}");
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Test user without role can access add profile form.
|
||||
// Expected: User cannot access form.
|
||||
$web_user1->removeRole($this->role2);
|
||||
$web_user1->save();
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}");
|
||||
$this->assertResponse(403);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
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 profiles',
|
||||
'administer profile types',
|
||||
'bypass profile access',
|
||||
'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', array(
|
||||
':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]['href'] == $expected ? 'pass' : 'fail');
|
||||
$this->{$method}(new FormattableMarkup('Task @number href @value equals @expected.', [
|
||||
'@number' => $index + 1,
|
||||
'@value' => (string) $elements[$index]['href'],
|
||||
'@expected' => $expected,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Tests profile access handling.
|
||||
*/
|
||||
abstract class ProfileTestBase extends WebTestBase {
|
||||
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
|
||||
/**
|
||||
* 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',
|
||||
'bypass profile access',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 testCRUDUI() {
|
||||
$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->randomString();
|
||||
$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]));
|
||||
$edit = [
|
||||
'registration' => 1,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertUrl('admin/config/people/profiles/types');
|
||||
$this->assertRaw(new FormattableMarkup('%label profile type has been updated.', ['%label' => $label]));
|
||||
|
||||
\Drupal::service('entity_type.bundle.info')->clearCachedBundles();
|
||||
|
||||
// Add a field to the profile type.
|
||||
$this->drupalGet("admin/config/people/profiles/types/manage/$id/fields/add-field");
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_label = $this->randomString();
|
||||
$edit = [
|
||||
'new_storage_type' => 'string',
|
||||
'label' => $field_label,
|
||||
'field_name' => $field_name,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
|
||||
$this->drupalPostForm(NULL, [], t('Save field settings'));
|
||||
$this->drupalPostForm(NULL, [], t('Save settings'));
|
||||
$this->assertRaw(new FormattableMarkup('Saved %label configuration.', ['%label' => $field_label]));
|
||||
|
||||
// Verify that the field is still associated with it.
|
||||
$this->drupalGet("admin/config/people/profiles/types/manage/$id/fields");
|
||||
// @todo D8 core: This assertion fails for an unknown reason. Database
|
||||
// contains the right values, so field_attach_rename_bundle() works
|
||||
// correctly. The pre-existing field does not appear on the Manage
|
||||
// fields page of the renamed bundle. Not even flushing all caches
|
||||
// helps. Can be reproduced manually.
|
||||
// $this->assertText(check_plain($field_label));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\profile\Tests;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Tests multiple enabled profile types.
|
||||
*
|
||||
* @group profile
|
||||
*/
|
||||
class ProfileTypeMultipleTest extends ProfileTestBase {
|
||||
|
||||
/**
|
||||
* Tests the flow of a profile type that has multiple enabled.
|
||||
*/
|
||||
public function testMultipleProfileType() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$edit = [
|
||||
'multiple' => 1,
|
||||
];
|
||||
$this->drupalPostForm("admin/config/people/profiles/types/manage/{$this->type->id()}", $edit, t('Save'));
|
||||
$this->assertRaw(new FormattableMarkup('%type profile type has been updated.', [
|
||||
'%type' => $this->type->label(),
|
||||
]));
|
||||
|
||||
$web_user1 = $this->drupalCreateUser(
|
||||
[
|
||||
"view own {$this->type->id()} profile",
|
||||
"add own {$this->type->id()} profile",
|
||||
"edit own {$this->type->id()} profile",
|
||||
]
|
||||
);
|
||||
$this->drupalLogin($web_user1);
|
||||
$value = $this->randomMachineName();
|
||||
|
||||
$edit = [
|
||||
"{$this->field->getName()}[0][value]" => $value,
|
||||
];
|
||||
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}", $edit, t('Save'));
|
||||
$this->assertRaw(new FormattableMarkup('%type profile has been created.', [
|
||||
'%type' => $this->type->label(),
|
||||
]));
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type->id()}");
|
||||
$this->assertLinkByHref("user/{$web_user1->id()}/{$this->type->id()}/add");
|
||||
$this->assertText($value);
|
||||
|
||||
$value2 = $this->randomMachineName();
|
||||
$edit = [
|
||||
"{$this->field->getName()}[0][value]" => $value2,
|
||||
];
|
||||
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}/add", $edit, t('Save'));
|
||||
$this->assertRaw(new FormattableMarkup('%type profile has been created.', [
|
||||
'%type' => $this->type->label(),
|
||||
]));
|
||||
|
||||
Cache::invalidateTags(['profile_view']);
|
||||
|
||||
$this->drupalGet("user/{$web_user1->id()}/{$this->type->id()}");
|
||||
$this->assertText($value2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the non-multiple profile type create and edit flow.
|
||||
*/
|
||||
public function testProfileNotMultipleFlow() {
|
||||
$web_user1 = $this->createUser([
|
||||
"add own {$this->type->id()} profile",
|
||||
"edit own {$this->type->id()} profile",
|
||||
]);
|
||||
$this->drupalLogin($web_user1);
|
||||
|
||||
// Create the profile.
|
||||
$edit = [
|
||||
"{$this->field->getName()}[0][value]" => $this->randomString(),
|
||||
];
|
||||
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}", $edit, 'Save and make default');
|
||||
$this->assertRaw(new FormattableMarkup('%type profile has been created.', [
|
||||
'%type' => $this->type->label(),
|
||||
]));
|
||||
|
||||
// Update the profile.
|
||||
$edit = [
|
||||
"{$this->field->getName()}[0][value]" => $this->randomString(),
|
||||
];
|
||||
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}", $edit, t('Save'));
|
||||
$this->assertRaw(new FormattableMarkup('%type profile has been updated.', [
|
||||
'%type' => $this->type->label(),
|
||||
]));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user