This is closely related to #276153: Testing does not work at all when locale of host session is not English, however that bug was about DrupalWebTestCase (and the fix there also fixed only DrupalWebTestCase). I found that the same problem occurs with DrupalUnitTestCase.

This is on Drupal 7.16 under Debian, with php 5.3 (from Debian stable packages).

If your site default language is not English, then unit tests fail with error like this:

    PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table
    'wpae.simpletest867413locales_source' doesn't exist...

To reproduce: I attach a trivial testmodule that contains a simplest MyTestCase inside testmodule.test. Set your site language to non-English (I use Polish, code "pl"). Running

sudo -u www-data php scripts/run-tests.sh --verbose MyTests 

returns

Drupal test run
---------------

Tests to be run:
 - Some unit tests (MyTestCase)

Test run started:
 piątek, Październik 26, 2012 - 00:10

Test summary
------------

Some unit tests 0 zakończonych pomyślnie, 0 zakończonych niepomyślnie, i 1 wyjątek

Test run duration: 0 sek

Detailed test results
---------------------


---- MyTestCase ----


Status    Group      Filename          Line Function                            
--------------------------------------------------------------------------------
Exception Uncaught e database.inc      2139 PDOStatement->execute()            
    PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table
    'wpae.simpletest931462locales_source' doesn't exist: SELECT
    s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN
    {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE
    s.source = :source AND s.context = :context AND s.textgroup =
    'default'; Array
    (
        [:language] => pl
        [:source] => Value @value is TRUE.
        [:context] => 
    )
     w locale() (linia 684 z .../modules/locale/locale.module).

(Some of the messages above are still in Polish. When the language is English, the error doesn't occur at all, so I can't show you an error log in English :)

I fixed it locally by applying a patch to modules/simpletest/drupal_web_test_case.php , similar to the one from #276153: Testing does not work at all when locale of host session is not English, but now to the DrupalUnitTestCase class. I'm attaching this patch here too, as it may be useful for others. It fixes the issue, i.e. above test completes Ok.

But my attached patch is probably not a clean solution. It would be better if this hack (forcing language to English) be moved into the base DrupalTestCase class (maybe abstract setUp method implementation), and then only accessed by descendants like DrupalWebTestCase and DrupalUnitTestCase.

Comments

Anonymous’s picture

Version: 7.16 » 8.x-dev
Issue tags: +Needs backport to D7

We should fix 8.x-dev first.

sylvain lecoy’s picture

Issue summary: View changes

Works for me in Drupal 7.

berdir’s picture

Unit tests in 8.x shouldn't call any functions, so not sure if this is relevant there.

sylvain lecoy’s picture

Version: 8.x-dev » 7.x-dev
Status: Active » Reviewed & tested by the community
Issue tags: -Needs backport to D7

I think this is actually not relevant for drupal8 as well.

Also, works for me :p

Status: Reviewed & tested by the community » Needs work

The last submitted patch, drupal_web_test_case.php_.patch, failed testing.

sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.12 KB
sylvain lecoy’s picture

Status: Needs review » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

Don't we have to restore the original language in the tearDown() method (just like DrupalWebTestCase does)?

geek-merlin’s picture

Status: Needs review » Needs work

> Don't we have to restore the original language in the tearDown() method (just like DrupalWebTestCase does)?

Yes, seems so.

geek-merlin’s picture

Status: Needs work » Needs review
StatusFileSize
new1.88 KB

Copying verbatim from DrupalWebTestCase further simplifies the patch.

geek-merlin’s picture

This fixes the issue on my box.

geek-merlin’s picture

Status: Needs review » Needs work

Ah, only partially.

geek-merlin’s picture

Status: Needs work » Needs review
StatusFileSize
new1.99 KB

This does it.

geek-merlin’s picture

Patched and tested on a separate box.

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

So daring to rtbc.

The last submitted patch, 10: drupal-1823504-10-unittests-break-when-nonenglish.patch, failed testing.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x - thanks!

  • David_Rothstein committed 0528883 on 7.x
    Issue #1823504 by axel.rutz, michalis.kambi, Sylvain Lecoy: Unit tests (...

Status: Fixed » Closed (fixed)

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