Now that #1803674: Remove t() from default test assertions in TestBase class is in perhaps we can remove the following code:

    // @todo Remove this once this class has no calls to t() and format_plural()
    $this->container->setParameter('language.default_values', Language::$defaultValues);
    $this->container->register('language.default', 'Drupal\Core\Language\LanguageDefault')
      ->addArgument('%language.default_values%');
    $this->container->register('language_manager', 'Drupal\Core\Language\LanguageManager')
      ->addArgument(new Reference('language.default'));
    $this->container->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
      ->addArgument(new Reference('language_manager'));

Comments

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new1.17 KB

Let's see if anything breaks.

Status: Needs review » Needs work

The last submitted patch, 1: 2196241.1.patch, failed testing.

jhodgdon’s picture

Do you perhaps need to put the translation services into WebTestBase if you take them out of TestBase? It looks like the failing tests extend WebTestBase.

sun’s picture

Title: Remove translation services from TestBase » Remove string translation services from TestBase container

I'm spotting these causes:

  1. The Installer (web) tests are trying to generate form button label translations, but do not have a container available. Unlike WebTestBase, Drupal is not installed yet. We need to change those tests to actually resemble the minimal bootstrap container that is built in install_begin_request(), ideally even with the identical logic for discovering string translations. Before attempting that, it would be good to do #2175459: Convert Drupal\system\Tests\InstallerTest into a new Drupal\simpletest\InstallerTestBase first.
  2. The Drupal\system\Tests\Plugin\* classes appear to be unit tests. I suspect they're using t() in assertion messages.
  3. Ditto for Drupal\system\Tests\Common\SizeUnitTest.

I would actually recommend to remove these entire lines, too:

  $this->container = new ContainerBuilder();
  \Drupal::setContainer($this->container);

Instead, the patch in #2171315: Ensure a NULL container in UnitTestCase::setUP will allow us to simply nuke the global state container:

  \Drupal::setContainer(NULL);
alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new5.87 KB
new8.27 KB

Actually Drupal\system\Tests\Plugin\* and Drupal\system\Tests\Common\SizeUnitTest have dependencies on the string_translation service. The former because of the @Translation annotation and the latter due to format_plural() being used in format_size().

Patch just creates the services in the tests as necessary.

Leaving the installer test alone till #2175459: Convert Drupal\system\Tests\InstallerTest into a new Drupal\simpletest\InstallerTestBase gets in.

sun’s picture

I wonder whether these are appropriate unit tests in the first place?

If a test needs a service container, then it ought to be DUTB?

→ Let's simply change all of those into DUTB?


Closely related side-note: My ultimate goal for D8 is to remove UnitTestBase prior to 8.0. Unit tests should exclusively be PHPUnit tests only. That's going to leave us with DrupalUnitTestBase + WebTestBase for the non-unit scenarios.

(At that point, we could consider to rename DrupalUnitTestBase into UnitTestBase, although I'm not sure whether that's a good idea. A name like ExtensionTestBase or KernelTestBase would probably be more accurate.)

alexpott’s picture

StatusFileSize
new7.76 KB
new10.09 KB

Good idea - interdiff is to patch in #1

Status: Needs review » Needs work

The last submitted patch, 7: 2196241.7.patch, failed testing.

The last submitted patch, 5: 2196241.5.patch, failed testing.

sun’s picture

Assigned: Unassigned » sun
Status: Needs work » Needs review
Issue tags: +Testing system
StatusFileSize
new3.67 KB

Last patch was not the patch you intended to upload; that's why I canceled it ;)

Attached patch changes those tests to DUTB.

Also added a @todo for #2171315: Ensure a NULL container in UnitTestCase::setUP, even though I'd actually prefer to get that in first, so we can nuke $this->container entirely here.

Did not touch the installer tests either, waiting for #2175459: Convert Drupal\system\Tests\InstallerTest into a new Drupal\simpletest\InstallerTestBase

Status: Needs review » Needs work

The last submitted patch, 10: drupal8.testbase-container.10.patch, failed testing.

The last submitted patch, 10: drupal8.testbase-container.10.patch, failed testing.

dawehner’s picture

I wonder whether these are appropriate unit tests in the first place?

If a test needs a service container, then it ought to be DUTB?

→ Let's simply change all of those into DUTB?

Well, we have unit-testable code in the background for format_plural() but not for format_size(), see #2157945: Deprecate format_size() and use Drupal\Core\StringTranslation\ByteSizeMarkup instead
PluginTestBase certainly can be tested using pure unit tests.

Let's do that all in follow ups.

sun’s picture

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new3.86 KB

Re-rolled. Let's see what remains.

Status: Needs review » Needs work

The last submitted patch, 15: drupal8.testbase-container.15.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new6.5 KB
new2.48 KB

Status: Needs review » Needs work

The last submitted patch, 17: drupal8.testbase-container.17.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new11.07 KB
new4.55 KB
  1. Fixed HtmlIdentifierUnitTest.
  2. Fixed TableSortExtenderUnitTest.
  3. Fixed DiscoveryTestBase.
  4. Fixed InstallerTestBase requires a minimal mock container in order to issue requests.
sun’s picture

Let's quickly move forward here to prevent a new test to introduce a new dependency on the minimal mock container of TestBase! :-)

sun’s picture

19: test.translation.19.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 19: test.translation.19.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new11.07 KB

Merged HEAD.

sun’s picture

23: test.translation.23.patch queued for re-testing.

sun’s picture

23: test.translation.23.patch queued for re-testing.

sun’s picture

23: test.translation.23.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 23: test.translation.23.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new11.1 KB

Merged 8.x.

znerol’s picture

Great! This is almost identical to #2275965: Remove all services from the TestBase container.

  1. +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
    @@ -1229,7 +1209,7 @@ private function restoreEnvironment() {
    -    $this->container->set('current_user', $this->originalUser);
    +    \Drupal::getContainer()->set('current_user', $this->originalUser);
    

    Can we remove that line entirely? This operates on the original container. I guess there is no way that this service is tampered with from within tests?

  2. +++ b/core/modules/system/lib/Drupal/system/Tests/Common/HtmlIdentifierUnitTest.php
    @@ -7,13 +7,12 @@
    +class HtmlIdentifierUnitTest extends DrupalUnitTestBase {
    
    +++ b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php
    @@ -8,12 +8,12 @@
    +class SizeUnitTest extends DrupalUnitTestBase {
    
    +++ b/core/modules/system/lib/Drupal/system/Tests/Common/TableSortExtenderUnitTest.php
    @@ -7,13 +7,13 @@
    +use Drupal\simpletest\DrupalUnitTestBase;
    ...
    +class TableSortExtenderUnitTest extends DrupalUnitTestBase {
    
    +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/Discovery/DiscoveryTestBase.php
    @@ -7,12 +7,12 @@
    +abstract class DiscoveryTestBase extends DrupalUnitTestBase {
    
    +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php
    @@ -17,7 +17,15 @@
    +abstract class PluginTestBase extends DrupalUnitTestBase {
    

    Can we use KernelTestBase instead?

sun’s picture

StatusFileSize
new10.5 KB
new4.35 KB
  1. Removed current_user futzing in TestBase::restoreEnvironment().
  2. Converted DUTB to KTB for affected tests.
znerol’s picture

Status: Needs review » Reviewed & tested by the community

Great. Running a couple of unit-tests and web-tests through the UI did not reveal any problems, this is ready for inclusion.

tstoeckler’s picture

+++ b/core/modules/simpletest/src/TestBase.php
@@ -1071,29 +1071,9 @@ private function prepareEnvironment() {
+    // Ensure there is no service container.
+    $this->container = NULL;
+    \Drupal::setContainer(NULL);

As \Drupal::getContainer() is documented to return a ContainerInterface, I think this is slightly weird. Is there any value in NULL over new ContainerBuilder()?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 30: test.translation.30.patch, failed testing.

znerol’s picture

Issue tags: +Needs reroll
sun’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new9.59 KB

Merged 8.x.

@tstoeckler: Passing a NULL value is explicitly supported and desired in this case, because we do not want to build + set a new container, but instead, we explicitly want to reset the container in \Drupal to NULL.

Status: Needs review » Needs work

The last submitted patch, 35: test.translation.35.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new10.33 KB
new1.06 KB

Prevent var_export() in default assertion message from getting invoked in DiscoveryTestBase.

It looks like the plugin discovery tests did not run against the actual plugin_test.module plugins in HEAD. Otherwise, the var_export() PHP warning would appear in HEAD already.

With that, we should be back to RTBC.

sun queued 37: test.translation.37.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 37: test.translation.37.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new10.38 KB

Merged 8.x. Back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 40: test.translation.40.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new10.24 KB

Merged 8.x.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

  • catch committed 48fb187 on 8.x
    Issue #2196241 by sun, alexpott: Remove string translation services from...

Status: Fixed » Closed (fixed)

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