Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Validate XLIFF translation files #17903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from

Conversation

javiereguiluz
Copy link
Member

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

In #17902 @stof proposed to add a simple test to validate .xlf files (to avoid issues like #17893).

This is a proposal for that test. My questions:

1) Do you agree displaying detailed error messages when the XML is not valid. Example output for the #17893 error:

error_log

Or is it enough displaying just the PHPUnit error message and let the user figure out the details?

simple_error

2) How do I validate the translation files of the Security and Form components? Do I just copy+paste this test in those components?

@linaori
Copy link
Contributor

linaori commented Feb 23, 2016

You could add the tests to the phpunit bridge, imo the WebTestCase and KernelTestCase should go there too. Additionally any other TestCase files that are an extension point should be moved there or a special package for this.

@nicolas-grekas
Copy link
Member

@javiereguiluz detailed error messages look the more useful to me


namespace Symfony\Component\Validator\Tests\Resources;

class ConstraintTest extends \PHPUnit_Framework_TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class name doesn't equal the filename TranslationFilesTest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

public function setUp()
{
libxml_use_internal_errors(true);
libxml_clear_errors();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't you do this in the foreach loop below ?
I'd suggest also to move libxml_use_internal_errors(true); in the test case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you.

@stof
Copy link
Member

stof commented Feb 23, 2016

Additionally any other TestCase files that are an extension point should be moved there or a special package for this.

This is not always a good idea. For instance, we have a base test case for ConstraintValidator in the Validator component. Moving it to the bridge would be a pain, as the logic in the testcase depends on the version of the component. So I would vote against moving everything to the bridge.

public function testXlfFilesAreValid()
{
$translationFiles = glob(__DIR__.'/../../Resources/translations/*.xlf');
foreach ($translationFiles as $filePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact, would be much better with a data collector provider

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think he meant a data provider, not a data collector. And this is indeed a good idea as a failure in a file would not prevent validating subsequent files (as they would be separate tests)

$this->renderErrors($errors);
}

$this->assertEmpty($errors, sprintf('The "%s" file is not a valid XML file.', realpath($filePath)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to remove renderErrors, and replace $this->assertEmpty(...); by
$this->assertSame(array(), $errors); so that phpunit handles the output

@stof
Copy link
Member

stof commented Feb 23, 2016

How do I validate the translation files of the Security and Form components? Do I just copy+paste this test in those components?

Copy-pasting is probably the easiest here, yes. Moving this to the bridge will mean exposing it as a part of our API, which does not seem useful yet.

a way to simplify the copy-pasted logic would be to use PHPUnit_Util_Xml::loadFile to perform the loading. When putting it in strict mode, it will throw a PHPUnit_Framework_Exception when the file is invalid, and include the errors in the exception message. You just have to catch this exception and use $this->fail($e->getMessage()) then.

@javiereguiluz
Copy link
Member Author

Refactored as suggested by Nicolas and now I'm going to try @stof's idea.

@javiereguiluz
Copy link
Member Author

@stof I've applied your ideas and the tests now look very simple. I've also copy+pasted the test in the 3 components. Thanks!

/**
* @dataProvider provideTranslationFiles
*/
public function testTranslationFilesAreValid($filePath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, this test concerns a single file, so you should use a singular in the test name (yeah, this is nit-picking 😄 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done the renaming.

@stof
Copy link
Member

stof commented Feb 23, 2016

👍 for this. And we indeed don't need to extract this to the bridge, given how simple the logic is now.

@javiereguiluz
Copy link
Member Author

I've removed the try ... catch and the test seems to work...

output_correct

...and the output is reasonably good when there is an error:

output

@nicolas-grekas
Copy link
Member

👍

@stof
Copy link
Member

stof commented Feb 23, 2016

👍 (we may need the same test in one more places in Symfony 2.7+ in the Security component btw)

@fabpot
Copy link
Member

fabpot commented Feb 23, 2016

Thank you @javiereguiluz.

fabpot added a commit that referenced this pull request Feb 23, 2016
This PR was squashed before being merged into the 2.3 branch (closes #17903).

Discussion
----------

Validate XLIFF translation files

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

In #17902 @stof proposed to add a simple test to validate `.xlf` files (to avoid issues like #17893).

This is a proposal for that test. My questions:

**1)** Do you agree displaying detailed error messages when the XML is not valid. Example output for the #17893 error:

![error_log](https://cloud.githubusercontent.com/assets/73419/13250664/932d2f14-da2b-11e5-8ea0-ecb43f58feea.png)

Or is it enough displaying just the PHPUnit error message and let the user figure out the details?

![simple_error](https://cloud.githubusercontent.com/assets/73419/13250671/a3b4bfd2-da2b-11e5-9277-454d0dd160b2.png)

**2)** How do I validate the translation files of the Security and Form components? Do I just copy+paste this test in those components?

Commits
-------

db03055 Validate XLIFF translation files
@fabpot fabpot closed this Feb 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants