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.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | drupal-1823504-13-unittests-break-when-nonenglish.patch | 1.99 KB | geek-merlin |
Comments
Comment #1
Anonymous (not verified) commentedWe should fix 8.x-dev first.
Comment #2
sylvain lecoy commentedWorks for me in Drupal 7.
Comment #3
berdirUnit tests in 8.x shouldn't call any functions, so not sure if this is relevant there.
Comment #4
sylvain lecoy commentedI think this is actually not relevant for drupal8 as well.
Also, works for me :p
Comment #6
sylvain lecoy commentedComment #7
sylvain lecoy commentedComment #8
David_Rothstein commentedDon't we have to restore the original language in the tearDown() method (just like DrupalWebTestCase does)?
Comment #9
geek-merlin> Don't we have to restore the original language in the tearDown() method (just like DrupalWebTestCase does)?
Yes, seems so.
Comment #10
geek-merlinCopying verbatim from DrupalWebTestCase further simplifies the patch.
Comment #11
geek-merlinThis fixes the issue on my box.
Comment #12
geek-merlinAh, only partially.
Comment #13
geek-merlinThis does it.
Comment #14
geek-merlinPatched and tested on a separate box.
Comment #15
geek-merlinSo daring to rtbc.
Comment #17
David_Rothstein commentedCommitted to 7.x - thanks!