#2226533: Changes to the Language class due to the LanguageInterface (followup) is proving troublesome to get to RTBC

It has many small issues that when combined are proving difficult to get right, so the next step is to
harvest small parts of the patch in turn.

This issue is about extracting the parts that convert the weight property of language in a protected property.

This issue starts by extracting code from comment #244

This single strand from the other issue although small, will still touch many parts of the system hence "Will cause conflicts"

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

martin107’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
6.7 KB

First attempt, the possibility that I have missed something is high, but the fix should be obvious.

Please note the isset line in the getWeight() function.

Status: Needs review » Needs work

The last submitted patch, 1: weight-2304403-1.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
6.7 KB
745 bytes

Ahem the problem is simple .. if only I had seen it first time..

Status: Needs review » Needs work

The last submitted patch, 3: weight-2304403-3.patch, failed testing.

YesCT’s picture

martin107’s picture

Status: Needs work » Postponed
martin107’s picture

Status: Postponed » Needs review
FileSize
6.89 KB
1.18 KB

I am opening up this issue again.... it look like [ #2304651] may have solved the random fail issue.

In the long term the plan is :-
Apply its fix to all issues, and the EntityQueryTests should go green that will provide some confidence, BUT the next step,
is to run version of the patch 50 times over in #2254011: Drupal\system\Tests\Entity\EntityQueryTest random failure

I am just folding in the change here ... this issue has other problems to fix first.

Status: Needs review » Needs work

The last submitted patch, 7: weight-2304403-7.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
6.6 KB
682 bytes

Restored sort line to its original :-

Language::sort($this->languages);

Status: Needs review » Needs work

The last submitted patch, 9: weight-2304403-9.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
6.6 KB
475 bytes

Drupal\language\Tests\LanguageConfigurationTest now passes locally.

tstoeckler’s picture

+++ b/core/lib/Drupal/Core/Language/Language.php
@@ -166,7 +168,7 @@ public function setDirection($direction) {
-    return $this->weight;
+    return isset($this->weight) ? $this->weight : 0;

Why is this needed?

Since the property defaults to 0 it seems it shouldn't be.

martin107’s picture

@tstoeckler

I agree in part ....

Can I rephrase the question

What is to stop non-integer or NULL values being passed into the constructor or the setter?

The default values are made available for another programmer, but its use is not tightly enforced. There is no type checking. and nothing to stop the construction of a language with a weight not set ( ie set to null ). This will cause difficult to see issue in the long run.

What goes for weight, can also be said of locked ( https://www.drupal.org/node/2226533#comment-8942261 )

I have made the case in the past, in another issue, for these values to made-safe at point to intake. ( in setters or constructors )

But I was going for the simplest solution here, well easiest to review ... we have a maintenance problem at the moment as
this "ternary operator" checking is made at two places in core and it seemed natural to consolidate the fixup in the getter.

Anyway I would like to hear what you think?

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Sure I'm not opposed to doing it that way. I also like type-checking for primitive types in constructors but that's not something we generally do in core. So I think it would be more consistent to just drop the ternary. But again, it's your patch, so feel free to keep it :-). If this comes back green it's RTBC, so marking as such, as the testbot will set me straight otherwise.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: weight-2304403-11.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
2.05 KB
7.25 KB

To cut away at my own argument

$language_entity->weight = isset($language->weight) ? $language->weight : 0;

occurs at only ONE place language_save :-

  $language_entity->label = isset($language->name) ? $language->name : '';
  $language_entity->direction = isset($language->direction) ? $language->direction : '0';
  $language_entity->locked = !empty($language->locked);
  $language_entity->weight = isset($language->weight) ? $language->weight : 0;
  $language_entity->setDefault(!empty($language->default));
  $language_entity->save();

Other properties may be typed check in multiple places ( I am looking at you locked ) but not weight.

So I am returning getWeight() to the more consistent form..
[ I cannot use isset(func()) so I am using null !== func() instead. Its is PHP thing.]

It is a truism that many of my concerns about loose type checking in core could be resolved by providing better documentation above the constructor...
BUT even I think this would be overkill in this case ... using Language::defaultValues is a specialist thing that not many developers down the line
are going to want to do and for those that do there are plenty of examples in core for people to copy..

-----------------------------

I have fixed a minor nit pick in EntityTranslationTest but it is not the complete story, errors persist.

Status: Needs review » Needs work

The last submitted patch, 16: weight-2304403-16.patch, failed testing.

martin107’s picture

Assigned: martin107 » Unassigned

I will look at this again soon, but maybe not until the weekend

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: weight-2304403-16.patch, failed testing.

martin107’s picture

Status: Needs work » Postponed

I am postponing because I am hopeful, that #2226533: Changes to the Language class due to the LanguageInterface (followup) is on its way to being committed.

martin107’s picture

Status: Postponed » Needs work
martin107’s picture

Status: Needs work » Needs review
FileSize
6.68 KB

Patch no longer applied. Reroll.

Status: Needs review » Needs work

The last submitted patch, 23: weight-2304403-23.patch, failed testing.

martin107’s picture

EntityTranslationTest can be made to pass by changing the definition of Drupal\Core\Language\Language::weight from
protected to public,

So it looks like not all code of the form language->weight has been changed to language->getWeight()

I have looked but cannot find it. :(

martin107’s picture

Status: Needs work » Needs review
FileSize
6.73 KB

Needed reroll.

Status: Needs review » Needs work

The last submitted patch, 26: weight-2304403-26.patch, failed testing.

martin107’s picture

FileSize
7.28 KB
792 bytes

The original intent behind this issue is still valid.

#2226533: Changes to the Language class due to the LanguageInterface (followup) is too large and too cumbersome to get reviewed... so splitting off small segments that are obviously right, and can be reviewed faster.

Rerolling is complete .. NowI have finally fixed the failing tests by copying over changes from the parent issue.

martin107’s picture

Status: Needs work » Needs review

martin107 queued 28: weight-2304403-28.patch for re-testing.

martin107’s picture

Reroll triggered by

#2332739: Remove SortArray::sortByWeightAndTitle

as is to be expected a file diff between #28 and #31 shows only a simplification of Drupal\Core\Language\Language::sort()

PS I am removing the will case commit conflicts tags .. it was inherited from its parent which typically needs a daily reroll.. This issue is much smaller and has gone for weeks without a reroll...

YesCT’s picture

+++ b/core/modules/language/language.module
@@ -430,7 +430,7 @@ function language_save($language) {
-  $configurable_language->weight = isset($language->weight) ? $language->weight : 0;
+  $configurable_language->weight = (null !== $language->getWeight()) ? $language->getWeight() : 0;

weight has to have a value now, so we dont need the ternary.

We can do something like the other issue was.

(I guess it is possible that another class would implement LanugageInterface and not have an initial value on its weight property, or do some other crazy implementation. But I think we can expect other implementations to be such that getWeight will return a value. Cause we dont do this in other places where we use the value returned by getWeight.

YesCT’s picture

Status: Needs review » Needs work
martin107’s picture

Patch no longer applies.

@YestCT I will remove that ternary ... later ( tonight )

Just to cross reference, this other active issue, which I think I agree with.. aims to remove the setWeight()

So in the future the patch here may become even smaller.

martin107’s picture

Status: Needs work » Needs review
FileSize
5.79 KB

Just a straight reroll, for now.

one conflict which was trivial to resolve.

martin107’s picture

Assigned: martin107 » Unassigned
FileSize
5.75 KB
851 bytes

Snip snip ..now using $language->getWeight() without protection.

martin107’s picture

Needs a reroll... there is a sprint so I am just signalling that I am working on this now.

martin107’s picture

Assigned: Unassigned » martin107
martin107’s picture

Assigned: martin107 » Unassigned
FileSize
4.11 KB

patch is simpler because common.inc langauge_save() has gone (Yay!)

Désiré’s picture

Rerolled patch from #39.

YesCT’s picture

Status: Needs review » Needs work

I think we should protect the ConfigurableLanguage weight property also. (alexpott asked for the simultaneous change in Language and ConfigurableLanguage on another similar issue).

martin107’s picture

Status: Needs work » Needs review
FileSize
5.02 KB
1.19 KB

Hurumph

I could only see one place to alter, other than the definition itself.

I guess testbot will give me a better answer.

Status: Needs review » Needs work

The last submitted patch, 42: 2304403-protect_language_weight-42.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
5.07 KB
682 bytes

LanguageConfigurationTest now passes.

YesCT’s picture

FileSize
5.25 KB

a reroll (for #2340571: LanguageInterface needs isLocked method for the locked property.)
let's see how it looks when the testbot runs, and then we can look in detail.

YesCT’s picture

Issue tags: +Amsterdam2014
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Awesome!

+++ b/core/modules/language/src/ConfigurableLanguageManager.php
@@ -337,8 +337,8 @@ public function updateLockedLanguageWeights() {
+      if (!$language->isLocked() && $language->getWeight() > $max_weight) {
+        $max_weight = $language->getWeight();
       }

Follow up suggestion: #2350941: Simplify $language max weight code using max()

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed c52c5f9 and pushed to 8.0.x. Thanks!

  • alexpott committed c52c5f9 on 8.0.x
    Issue #2304403 by martin107, YesCT, Désiré: Convert language:weight into...
YesCT’s picture

Status: Fixed » Closed (fixed)

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