Problem/Motivation

Constants in bootstrap.inc are difficult to test and require weird hacks in our unit tests. When the constant is on a Class or interface they can be autoloaded and provide additional context about their purpose and meaning.

UserTest documents that this should be done as well but there is no issue attached to the @todo.

Proposed resolution

Move them to RoleInterface?

Remaining tasks

Provide a Patch

API changes

2 Constants are being renamed placed on and Interface instead of existing in bootstrap.inc

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task. This is part of our modernization and not providing a new feature or fixing any bugs
Issue priority Not critical. Have significant usability gains or community consensus that I know of.
Unfrozen changes ?
Disruption Could be disruptive for core/contrib because it changes constants that get a fair amount of us. It could be mitigated by providing BC in the form of a deprecated global constant.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

neclimdul’s picture

I'd like to say this is unfrozen because there was an existing @todo but it was hidden in a test and not clearly documented around the constants that feels a bit disingenuous.

neclimdul’s picture

Status: Active » Needs review
FileSize
96.21 KB

A possible implementation.

dawehner’s picture

index fcf4d44..3e62e4d 100644
--- a/core/includes/bootstrap.inc

--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc

+++ b/core/includes/bootstrap.inc
@@ -82,16 +82,6 @@

@@ -82,16 +82,6 @@
 const DRUPAL_BOOTSTRAP_FULL = 3;
 
 /**
- * Role ID for anonymous users; should match what's in the "role" table.
- */
-const DRUPAL_ANONYMOUS_RID = 'anonymous';
-
-/**
- * Role ID for authenticated users; should match what's in the "role" table.
- */
-const DRUPAL_AUTHENTICATED_RID = 'authenticated';
-

diff --git a/core/lib/Drupal/Core/Session/UserSession.php b/core/lib/Drupal/Core/Session/UserSession.php
index 6af360d..bc358e0 100644

index 6af360d..bc358e0 100644
--- a/core/lib/Drupal/Core/Session/UserSession.php

--- a/core/lib/Drupal/Core/Session/UserSession.php
+++ b/core/lib/Drupal/Core/Session/UserSession.php

+++ b/core/lib/Drupal/Core/Session/UserSession.php
@@ -7,6 +7,8 @@

@@ -7,6 +7,8 @@
 
 namespace Drupal\Core\Session;
 
+use Drupal\user\RoleInterface;
+

MHHHH, I think its wrong to add a dependency of user module onto all those code.

dawehner’s picture

Mabye \Drupal or AccountInterface is a better place for it?

daffie’s picture

Status: Needs review » Reviewed & tested by the community

+1 for the RoleInterface. It is where you would naturally expect it to be.

It all looks good to me.

Great work neclimdul!
Those horrible namespace constructions are gone.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 2433281-move_role_constants-2.patch, failed testing.

daffie’s picture

Issue tags: +Needs reroll
neclimdul’s picture

Status: Needs work » Needs review
FileSize
96.57 KB
3.23 KB

I think dawehner has a valid point. Referencing a module in Core is sort of like referencing a core class in Component, it exposes coupling (in this case unintended).

We had a talk in IRC and AccountInterface does sort of own the concept of this constant string. Its specifically a concept it owns an imposes on the RoleInterface so it does make some sense to put it there.

On the other hand, most of the systems interacting with this are interacting with the Role system, not the session system.

So I propose a middle ground where AccountInterface owns the constant and RoleInterface exposes it to the rest of our code.

Status: Needs review » Needs work

The last submitted patch, 8: 2433281-8.patch, failed testing.

neclimdul’s picture

Status: Needs work » Needs review
FileSize
96.92 KB
621 bytes

Woops, didn't look at the failure before. just assumed re-roll meant a conflict and rebased the new patch.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

It looks good to me.
Personally I would have moved the role-interface to Drupal\Core\Session\RoleInterface. But I can also live with the solution in the current patch.

Berdir’s picture

There's still the question of disruption/BC layer I guess. Based on some recent issues/feedback from core committers, a normal task isn't allowed to make any API changes.

Given that it would be trivial to keep the old constants around, I'd suggest to do that and deprecate them with removal in 9.x.

Note that I've run into a few such constants in my issue to remove the forced load of .module files, including those I think, so this would help that issue.

neclimdul’s picture

Issue tags: -Needs reroll
FileSize
97.29 KB
96.8 KB

Testbot hasn't noticed yet but this needed a reroll. Just some chunk conflicts like a use collision.

I'm ok with either of these patches. -deprecated has the old deprecated constant and the other doesn't. I'd prefer to get rid of them but understand if we want to be strict about our Beta requirements.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: 2433281-13.patch, failed testing.

neclimdul’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
96.2 KB

#2347625: Remove drupal_bootstrap and drupal_get_bootstrap_phase another chunk collision.

Deprecation is still an equal option but because it changed fewer lines it didn't have a problem with the bootstrap constant removal so I didn't re-roll it.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: 2433281-15.patch, failed testing.

neclimdul’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
671 bytes
96.24 KB

chunk conflict shown in mergediff.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We need to add the the fact the old constants are deprecated and will be removed in Drupal 9 to the old constants.

mrjmd’s picture

Status: Needs work » Needs review
FileSize
677 bytes
96.9 KB

Added deprecated notes as per #18

neclimdul’s picture

FileSize
97.55 KB
1.38 KB

Thanks!

The old constant is suppose to be an alias to the interface constant as in #13 but that got lost somehow.

Updated the @deprecated comment to point directly to the interfaces containing the constants too.

daffie’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Session/AccountInterface.php
@@ -18,6 +18,16 @@
+  const ANONYMOUS_ID = 'anonymous';
...
+  const AUTHENTICATED_ID = 'authenticated';

Can we rename them to ANONYMOUS_RID and AUTHENTICATED_RID. They are defined in the AccountInterface and the constants are related to roles not accounts.

mrjmd’s picture

Status: Needs work » Needs review
FileSize
97.74 KB
83.35 KB

Fully renamed the new constants as suggested in #21.

Status: Needs review » Needs work

The last submitted patch, 22: 2433281-22.patch, failed testing.

neclimdul’s picture

I disagree. The R in RID is to clarify what the ID is to be used for. In the patch the ID isn't always for roles, and the interface clarifies what the ID is for.

daffie’s picture

@neclimdul: And what if we rename them to AccountInterface::AUTHENTICATED_ROLE and AccountInterface::ANONYMOUS_ROLE?

neclimdul’s picture

Status: Needs work » Needs review
FileSize
97.8 KB
4.1 KB

I'm not sure I agree but a name is a name, lets just wrap this up.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
@@ -157,28 +158,8 @@ public function testHasPermission($permission, array $sessions_with_access, arra
-namespace {
-
-  if (!defined('DRUPAL_ANONYMOUS_RID')) {
-    /**
-     * Stub Role ID for anonymous users since bootstrap.inc isn't available.
-     */
-    define('DRUPAL_ANONYMOUS_RID', 'anonymous');
-  }
-  if (!defined('DRUPAL_AUTHENTICATED_RID')) {
-    /**
-     * Stub Role ID for authenticated users since bootstrap.inc isn't available.
-     */
-    define('DRUPAL_AUTHENTICATED_RID', 'authenticated');
-  }
-
-}

If we want to move to doing browser testing using PHPUnit then we have to remove all of this type of thing before release.

Therefore considering that this change allows for that and leaves a backwards compatibility layer in, I think that this change is good to make during beta.Thanks for adding the beta evaluation to the issue summary. Committed 59388d8 and pushed to 8.0.x. Thanks!

  • alexpott committed 59388d8 on 8.0.x
    Issue #2433281 by neclimdul, mrjmd: Move Role Constants on to a Class/...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Mile23’s picture

Issue tags: +@deprecated

Edited this change record to reflect this new deprecation: https://www.drupal.org/node/1619504