Writing test cases for a WordPress Plugin that has translations

If all that you want to test is that the translation files exist, then this is probably the simplest approach, since you’ll probably already be using PHPUnit to run unit/integration tests on the plugin code.

However, just checking whether the files exist doesn’t tell you much. It doesn’t tell you whether those languages are actually fully translated, or even if those files will be loaded properly by WordPress.

So the question you need to ask yourself is, “Why am I testing this?” If you just want to make sure that you don’t accidentally leave out a translation file, then your tests should do what you need. If you want to actually ensure that you have complete translations for each language, and that these actually work, that’s an entirely different story.

Testing translation loading

To check whether each translation file can be properly loaded by WordPress, you could probably create a PHPUnit test that loaded the files using load_plugin_textdomain(), and checking that it returns true, which would indicate that the textdomain could be properly loaded. You’ll probably need to hook into the plugin_locale filter to check each locale that ships with your plugin.

Testing completeness

However, that still wouldn’t tell you how many of the strings were actually translated into that language, how many are fuzzy, etc. If you want to check that each translation is at least xx% complete, you’ll probably want to use a tool other than PHPUnit for that.

Testing appearance

You could also go further and check that the strings in each translation actually appear correctly in the plugin UI. Sometimes a string might be much longer in a translation than it is in the original language, and this can cause it to overflow the area allotted for it. To check this you’d need to use something like Codeception acceptance tests.

Conclusion

So, in conclusion, whether the tests you have are the right approach for you depends on what you want to test. Personally, I don’t have any tests for my plugins’ translations, although that last part about checking the UI has intrigued me. Just checking that the translations exist, as you are doing now, doesn’t appear to me to provide much benefit. It is a check that is only as good as your list of translation files that you are checking for. And in one sense it doesn’t actually test anything, just makes sure that it is there. That could be performed in a build script rather than PHPUnit. Checking that the translations can actually be loaded by WordPress does provide benefit IMO, because it actually tests that they aren’t corrupted. And of course, it tests that they exist at the same time. So I’d probably expand your tests to check that too.

Edit

On a related note, you might also consider testing for any spelling errors in your main POT (and in translations too, I guess). It’s easy for these to slip in under the radar. Probably there are shell scripts available that could do this.

Leave a Comment