Follow-up #2881030-15: $total_items can be negative if pager has offset.
@Manuel Garcia:

Perhaps tackling this problem on a different issue for all core tests would make sense?

Problem/Motivation

  • Better IDE supports
  • Shorter (in most cases)
  • We already do this when we add a new code

Proposed resolution

  • Static case:
    +use Drupal\block\Entity\Block;
    ...
    -    $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
    +    $entity = $this->getMockBuilder(Block::class)
  • Collisions case (full path):
    ...
    use Drupal\migrate\Plugin\migrate\destination\Config;
    ...
    -    $config = $this->getMockBuilder('Drupal\Core\Config\Config')
    +    $config = $this->getMockBuilder(\Drupal\Core\Config\Config::class)
    
  • Dynamic case (don't change):
    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name);

Remaining tasks

  • Evaluate the proposal and find out the scope
  • Write a script for maximum automation of changes
  • Add a rule to the Code Standards

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

vaplas created an issue. See original summary.

Anonymous’s picture

Status: Active » Needs review
FileSize
785.4 KB
5 KB

Reproduce script:

  1. composer require grom358/pharborist
  2. move script to root of site
  3. run script

We have 4 files with collisions of names:

  • core\modules\migrate\tests\src\Unit\destination\ConfigTest.php
  • core\modules\views\tests\src\Unit\Plugin\argument_validator\EntityTest.php
  • core\tests\Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest.php
  • core\tests\Drupal\Tests\Core\Menu\LocalActionManagerTest.php

Now the script use the full signature in these cases, like

-    $config = $this->getMockBuilder('Drupal\Core\Config\Config')
+    $config = $this->getMockBuilder(\Drupal\Core\Config\Config::class)
Manuel Garcia’s picture

Issue tags: +phpunit initiative

Brilliant @vaplas!

Adding tag for visibility =)

Manuel Garcia’s picture

Status: Needs review » Needs work

Applied the patch, can still find uncovered tests in:

  • core/modules/migrate/tests/src/Unit/process/MigrateProcessTestCase.php
  • core/modules/views/tests/src/Unit/Plugin/HandlerTestTrait.php
  • core/tests/Drupal/Tests/Core/Form/FormTestBase.php
  • core/tests/Drupal/Tests/Core/Image/ImageTest.php
  • core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php
  • core/tests/Drupal/Tests/Core/Render/RendererTestBase.php
  • core/tests/Drupal/Tests/UnitTestCase.php

Almost there!

Anonymous’s picture

Issue summary: View changes

Thanks for review and motivation, @Manuel Garcia!

Yeah, now the script worked only with files that end on '..Test.php', not TestTrait.php, TestCase, etc. Good points, will be fixed.

# core/tests/Drupal/Tests/Core/Image/ImageTest.php:
  protected function getToolkitOperationMock($class_name, ImageToolkitInterface $toolkit) {
    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name);

It seems we should replace only static cases. IS updated.

Also current script replaces only next methods:

  • getMock
  • getMockBuilder
  • getMockForAbstractClass
  • getMockForTrait
  • prophesize
  • setClass
  • setExpectedException

Maybe something else too?

Anonymous’s picture

Issue summary: View changes
Manuel Garcia’s picture

Status: Needs work » Needs review
FileSize
18.75 KB
804.14 KB

I agree about ImageTest::getToolkitOperationMock().

I couldn't help myself and fixed the listed remaining by hand, which thinking now about it may not have been such a great idea... anyway here it is :)

Manuel Garcia’s picture

I can see uses still of getMockBuilder on:

  • core/modules/user/tests/src/Unit/Plugin/Action/RoleUserTestBase.php
  • core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php

setClass has uses still on:

  • core/core.api.php
  • core/lib/Drupal/Core/CoreServiceProvider.php
  • core/lib/Drupal/Core/Entity/entity.api.php
  • core/lib/Drupal/Core/Installer/InstallerServiceProvider.php
  • core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php
  • core/modules/language/src/LanguageServiceProvider.php
  • core/modules/system/tests/modules/service_provider_test/src/ServiceProviderTestServiceProvider.php
  • core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
  • core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php
  • core/modules/user/src/Entity/User.php

getMockForAbstractClass, getMockForTrait, prophesize and setExpectedException seem to be done (yay!)

Status: Needs review » Needs work

The last submitted patch, 7: 2886352-7.patch, failed testing. View results

Manuel Garcia’s picture

Status: Needs work » Needs review
FileSize
627 bytes
804.14 KB

Missing semicolon... /me hides

Anonymous’s picture

#7: Yeah!)

#8 good research!

Research into the possibility of replacing all strings (if they are existing classes, interfaces or traits) revealed a same discrepancy (see fails.patch).

Also, the current script has a number of shortcomings:

  • does not check namespace of the class (which leads to unnecessary 'use' import)
  • does not check aliases
  • ignores 'use' with alias.

But I'll try to fix it in the future.

Status: Needs review » Needs work

The last submitted patch, 11: 2886352-11-fails.patch, failed testing. View results

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.

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.

borisson_’s picture

We should probably postpone this issue on #2929133: Replace all usages of getMock(), because the conflicts will be in just about every line of this patch.

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.

dawehner’s picture

Is there any way how we could introduce some form of phpcs.xml.dist check for this?

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.

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.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.