API page: https://api.drupal.org/api/drupal/core%21modules%21update%21update.modul...

The function uses the following code.

$last_email_notice = \Drupal::state()->get('update.last_email_notification') ? : 0;

This is not necessary, since \Drupal::state()->get() accepts a default value as second parameter. With the current code, the returned value is checked twice to see if it is NULL: first from \Drupal::state()->get(), and then from update_cron().
It is enough using the following code.

$last_email_notice = \Drupal::state()->get('update.last_email_notification', 0);

If the value has not been already set, \Drupal::state()->get() will return the value passed as second parameter.

public function get($key, $default = NULL) {
  $values = $this->getMultiple(array($key));
  return isset($values[$key]) ? $values[$key] : $default;
}

Comments

kiamlaluno created an issue. See original summary.

avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

Status: Active » Needs review
StatusFileSize
new1.18 KB

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

avpaderno’s picture

StatusFileSize
new1.18 KB
avpaderno’s picture

Title: update_cron() doesn't pass to \Drupal::state()->get() the default value » In update_cron(), use the second parameter of \Drupal::state()->get() instead of checking the method didn't return any value
Category: Bug report » Task
Priority: Minor » Normal

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

avpaderno’s picture

Title: In update_cron(), use the second parameter of \Drupal::state()->get() instead of checking the method didn't return any value » Use the second parameter of \Drupal::state()->get() instead of checking the method didn't return any value
Issue tags: +Novice
StatusFileSize
new2.33 KB

There are other files where this is done. This patch fixes those files too (always for the Update module).

isholgueras’s picture

Status: Needs review » Reviewed & tested by the community

I've review it and tested. The patch applies fine and works great.

alexpott’s picture

Component: update.module » base system
Status: Reviewed & tested by the community » Needs work

This is the kind of thing that should be done consistently for the entirety of core as the per module issue scope is not the best. You can use a regex like [:>]state(\(\))?->get\(.*\? to find more. Note not all should be converted. For example, \Drupal::state()->get(static::class) ?: parent::getAll() should not be convert because you don't always want to call parent::getAll()

elaman’s picture

Status: Needs work » Needs review
StatusFileSize
new34.28 KB

I've created a patch based on @alexpott suggestions. Please review.

Status: Needs review » Needs work

The last submitted patch, 14: use-default-state-value-2778917-14.patch, failed testing. View results

elaman’s picture

Status: Needs work » Needs review
StatusFileSize
new33.56 KB

New patch, which accounts for CssCollectionRendererUnitTest.php expecting NULL.

alexpott’s picture

StatusFileSize
new3.9 KB
new37.98 KB

@Elaman CssCollectionRendererUnitTest is failing because it is not mocking correctly. We can fix it here.

Also there were a few left over in core that can be converted.

elaman’s picture

@alexplott I thought fixing CssCollectionRendererUnitTest might be a scope-creep.

Status: Needs review » Needs work

The last submitted patch, 17: 2778917-17.patch, failed testing. View results

alexpott’s picture

@Elaman I don't think so - the mock is incorrect and needs adjusting to work how the function actually works.

alexpott’s picture

Status: Needs work » Needs review

I can't reproduce the test fail locally. Retesting.

avpaderno’s picture

It seems there is a deprecation notice repeated twice.

Passing an escaped locator to the named selector is deprecated as of 1.7 and will be removed in 2.0. Pass the raw value instead. in EntityReferenceAdminTest::testFieldAdminHandler from Drupal\Tests\field\Functional\EntityReference

alexpott’s picture

@kiamlaluno yeah but I didn't get that when running the test locally and now DrupalCI is not reporting it either. It's an unrelated random.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

StatusFileSize
new38.28 KB
new38.28 KB
  • Went through the entire patch, every single change looks correct
  • Applied the patch locally, it didn't 100% apply anymore, so I rerolled
  • I tried to find more instances of the pattern but this seems still be all of them.
+++ b/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php
@@ -26,19 +27,13 @@ class CssCollectionRendererUnitTest extends UnitTestCase {
 
-  /**
-   * The state mock class.
-   *
-   * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
-   */
-  protected $state;
-
   protected function setUp() {
     parent::setUp();
 
-    $this->state = $this->getMock('Drupal\Core\State\StateInterface');
+    $state = $this->prophesize(StateInterface::class);
+    $state->get('system.css_js_query_string', '0')->willReturn('0');
 
-    $this->renderer = new CssCollectionRenderer($this->state);
+    $this->renderer = new CssCollectionRenderer($state->reveal());

That's much nicer!

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Forget to RTBC it.

The last submitted patch, 25: 2778917-25.patch, failed testing. View results

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 25: 2778917-25.patch, failed testing. View results

avpaderno’s picture

Now there are two deprecation notices about EntityManagerInterface::loadEntityByUuid(). It doesn't seem they are caused by the changes introduced in this patch.

vadim.hirbu’s picture

Status: Needs work » Needs review
StatusFileSize
new37.79 KB
new2.44 KB

Hi @kiamlaluno.
I found that your patch added changes to method loadEntityByUuid()

- $this->assertFalse(\Drupal::service('entity.repository')->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
-    $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
+    $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
+    $deleted_storages = \Drupal::state()->get('field.storage.deleted', []);

I've just updated the latest patch to not apply changes to this part \Drupal::service('entity.repository')->loadEntityByUuid().
Added interdiff file.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

tvb’s picture

Assigned: Unassigned » tvb
Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch from #30 could not be applied to 8.9.x-dev.

ravi.shankar’s picture

Assigned: tvb » ravi.shankar

Working on this.

ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new35.65 KB

Here I have added reroll of patch #30.

Status: Needs review » Needs work

The last submitted patch, 34: 2778917-34.patch, failed testing. View results

tvb’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new33.62 KB
new33.01 KB

There were 6 conflicts in the patch from #30.

Four of these were resolved easily. One concerned a deleted module (path_test).

The changes in CssCollectionRendererUnitTest (6th conflict) were rejected because they are not related to \Drupal::state()->get().

It seems the issue mentioned in #16 to #20 is fixed:

www-data@0ab40c65f6d4:/app/core$ ../vendor/bin/phpunit -c /app/core/phpunit.xml tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\Core\Asset\CssCollectionRendererUnitTest
.....                                                               5 / 5 (100%)

Time: 319 ms, Memory: 6.00MB

OK (5 tests, 11 assertions)

New patch and diff file attached.

alexpott’s picture

Version: 8.9.x-dev » 9.1.x-dev

@tvb I like this. But given it's a task the most likely branch for this to land in is 9.1.x - and the so let's roll the next patch against that branch.

jungle’s picture

StatusFileSize
new31.49 KB
new6.94 KB

Rerolled patch from #36

quietone’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed the patch and all changes look correct to me, and of course tests are passing. Applied the patch and used the regex from $13, which found 4 occurrences, none of which should be changed.

 egrep -r '[:>]state(\(\))?->get\(.*\?' core | nl
     1  core/modules/config/tests/config_override_integration_test/src/Cache/ConfigOverrideIntegrationTestCacheContext.php:    $state = \Drupal::state()->get('config_override_integration_test.enabled', FALSE) ? 'yes' : 'no';
     2  core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php:    return \Drupal::state()->get(static::class) ?: parent::getAll();
     3  core/modules/basic_auth/tests/modules/basic_auth_test/src/BasicAuthTestController.php:      '#markup' => \Drupal::state()->get('basic_auth_test.state.controller_executed') ? 'yep' : 'nope',
     4  core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php:    return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
jungle’s picture

StatusFileSize
new4.44 KB
new31.61 KB

@quietone, Thanks for reviewing!

rerolled a patch for 8.8.x in case it's needed.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 6b54e95 and pushed to 9.1.x. Thanks!

As a task this is only eligible for 9.1.x

  • alexpott committed 6b54e95 on 9.1.x
    Issue #2778917 by kiamlaluno, jungle, elaman, dawehner, alexpott, tvb,...

Status: Fixed » Closed (fixed)

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