user_entity_info() defines format_username($account) as the 'label callback' of the User entity. Unfortunately, a label callback expects $entity_type, $entity as parameters, not just the entity.

Major, as it breaks every code out there that tries to work with entities in a generic way.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
FileSize
2.49 KB

It's kind of obvious that you shouldn't change format_username as this would break a lot of things and it doesn't really make sense to have entity_type in a very specific user function.

Let's see whether the tests are running.

aspilicious’s picture

Status: Needs review » Needs work
+++ b/modules/user/user.testundefined
@@ -2169,3 +2169,45 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+}
\ No newline at end of file

Needs newline

Code looks ok but could you attach a test only file.
Thnx!

EDIT: Ok I missed the TODO in the tests

4 days to next Drupal core point release.

dawehner’s picture

Status: Needs work » Needs review
FileSize
1.55 KB

Here is the test patch

dawehner’s picture

And here comes the total patch.

yoroy’s picture

So will this make things show up in the UI that were previously hidden? If so, a screenshot would be much appreciated.

Status: Needs review » Needs work

The last submitted patch, 1261918-label_callback-user.patch, failed testing.

tstoeckler’s picture

+++ b/modules/user/user.test
@@ -2169,3 +2169,43 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+    $this->anonymous = user_load(0);

Interesting, never seen that. Is there a difference between that and drupal_anonymous_user()?

+++ b/modules/user/user.test
@@ -2169,3 +2169,43 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+    variable_set('anonymous', $name);
+    $this->assertEqual($name, entity_label('user', $this->account), 'The variable anonymous should be used for name of uid 0');

This should be $this->anonymous and not $this->account. That's test fail #1. This shows that the assertion works, which is good. :)

In general, I can't believe we/I missed this in #1096446: entity_label() is not passing along the $entity_type parameter, when looking for label callbacks I only searched for "label" on api.drupal.org, *slapsforehead*...

dawehner’s picture

Status: Needs work » Needs review
FileSize
2.47 KB
Interesting, never seen that. Is there a difference between that and drupal_anonymous_user()?

There is a huge difference internally, because user_load does provide a full user object while drupal_anonymous_user has just a small version of it. Let's use drupal_anonymous_user, as this will be probably used on real sites.

About the test failures, *facepalm*. When i made this, the tests runned locally not as well, but i thought that's a problem of my setup.
Okay here is a new one.

Status: Needs review » Needs work

The last submitted patch, 1261918-label_callback-user.patch, failed testing.

Bojhan’s picture

Wonder if yoroy's question still stands.

dawehner’s picture

About yoroy's question. Core doesn't use entity_label yet somewhere at all, so nothing changes for the user.

It's basically just an api which could be used on other modules.

Bojhan’s picture

Issue tags: +Quick fix

Oke, just needs someone to fix the test.

Damien Tournoud’s picture

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

This actually only applies to Drupal 8. Not sure how I managed to bump into in on Drupal 7... probably messed up with my development environment somehow.

catch’s picture

Status: Reviewed & tested by the community » Needs work

*d8 suffixed patches are ignored by the bot, and this patch has never passed yet, so can't be RTBC.

Bojhan’s picture

FileSize
2.54 KB
Bojhan’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Needs work
+++ b/modules/user/user.testundefined
@@ -2169,3 +2169,44 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+    variable_set('anonymous', $name);
+    $this->assert(strpos(entity_label('user', $this->account), $name) !== FALSE, t('The variable anonymous should be used for name of uid 0'));

This is probably an old patch, as checking for the name of the anonymous user but giving the registered account can't work :) Blame me. This was already fixed in #8 but is not part of #13

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
2.51 KB

Here's a new one. I actually edited the patch file, so things might blow up.

I fixed the test and also went back to using assertEqual() directly.

Status: Needs review » Needs work

The last submitted patch, 1261918.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
2.51 KB

Okay that was the D7 patch, in D8 the argument order is reversed.

This one should work.

tstoeckler’s picture

Status: Needs review » Needs work
+++ b/modules/user/user.module
@@ -192,6 +192,23 @@ function user_uri($user) {
+ * @param $entity
+ *   The entity object.
+ * @param $entity_type
+ *   The entity type.

And that should of course be reversed as well.

+++ b/modules/user/user.test
@@ -2169,3 +2169,44 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+      'description' => 'Tests specific parts of the user entity like the uri callback and the label callback.',

It should be URI callback, right?

+++ b/modules/user/user.test
@@ -2169,3 +2169,44 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+   * Test uri callback.

See above.

+++ b/modules/user/user.test
@@ -2169,3 +2169,44 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
+    // @todo: What is the wanted value of the anonymous user uri.

See above.

dawehner’s picture

Status: Needs work » Needs review
FileSize
2.51 KB

Oh thanks true.

Okay here is the patch for d8, let's get this ready first before continue to work on the d7 version.

tstoeckler’s picture

I don't think we need a D7 patch here.
In D7 $entity is passed as first argument so format_username() is fine as 'label callback'.

dawehner’s picture

Issue tags: +Needs backport to D7

Ah okay make sense, thanks.

In general the tests should be backported.

Dave Reid’s picture

With regards to the @todo in the test, user_uri() doesn't care if the user is anonymous or not. Either way it should return a value of 'user/0'.

dawehner’s picture

Let's remove the todo

catch’s picture

This looks good but there is a minor whitespace issue:

+ * @param $entity
+ *   The entity object.
+ *
+ * @return
+ *    The name of the user.
catch’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.45 KB

Fixed the whitespace myself. I'm marking this RTBC. If there's no objections before Friday I'll commit it then.

tstoeckler’s picture

RTBC++
Go for it! :)

chx’s picture

Looks good.

catch’s picture

Version: 8.x-dev » 7.x-dev
Priority: Major » Normal
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: -Quick fix

Thanks, two extras is plenty, so committed and pushed to 8.x.

Discussion here suggests there's no Drupal 7 bug here, but maybe we can backport the tests?

RobLoach’s picture

oriol_e9g’s picture

Status: Patch (to be ported) » Needs review
FileSize
1.54 KB

Tests backported and passed locally :D

USER ENTITY CALLBACK TESTS
Tests specific parts of the user entity like the URI callback and the label callback.
7 passes, 0 fails, and 0 exceptions
mgifford’s picture

mgifford’s picture

Issue summary: View changes
FileSize
3.31 KB

The patch in #33 only adds tests to modules/user/user.test

Doesn't actually fix the problem.

Status: Needs review » Needs work

The last submitted patch, 35: 1261918-35-label_callback-user.patch, failed testing.

  • catch committed feb983d on 8.3.x
    Issue #1261918 by dereine, tstoeckler, Damien Tournoud, Bojhan: Fixed...

  • catch committed feb983d on 8.3.x
    Issue #1261918 by dereine, tstoeckler, Damien Tournoud, Bojhan: Fixed...

  • catch committed feb983d on 8.4.x
    Issue #1261918 by dereine, tstoeckler, Damien Tournoud, Bojhan: Fixed...

  • catch committed feb983d on 8.4.x
    Issue #1261918 by dereine, tstoeckler, Damien Tournoud, Bojhan: Fixed...

  • catch committed feb983d on 9.1.x
    Issue #1261918 by dereine, tstoeckler, Damien Tournoud, Bojhan: Fixed...