TranslationFiles.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity\App\Language;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Filesystem\Driver\File;
  9. class TranslationFiles extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\File\Csv
  13. */
  14. protected $csvParser;
  15. protected function setUp()
  16. {
  17. $this->csvParser = new \Magento\Framework\File\Csv(new File());
  18. }
  19. /**
  20. * @return array
  21. */
  22. public function getLocalePlacePath()
  23. {
  24. $pathToSource = BP;
  25. $places = [];
  26. $componentRegistrar = new ComponentRegistrar();
  27. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
  28. $places[basename($modulePath)] = ['placePath' => $modulePath];
  29. }
  30. foreach ($componentRegistrar->getPaths(ComponentRegistrar::THEME) as $themePath) {
  31. $placeName = basename(dirname(dirname($themePath))) . '_' . basename($themePath);
  32. $places[$placeName] = ['placePath' => $themePath];
  33. }
  34. $places['lib_web'] = ['placePath' => "{$pathToSource}/lib/web"];
  35. return $places;
  36. }
  37. /**
  38. * @param string $modulePath
  39. * @return string[] Array csv files array[$locale]$pathToCsvFile]
  40. */
  41. protected function getCsvFiles($modulePath)
  42. {
  43. $files = [];
  44. foreach (glob("{$modulePath}/i18n/*.csv") as $file) {
  45. $locale = str_replace('.csv', '', basename($file));
  46. $files[$locale] = $file;
  47. }
  48. return $files;
  49. }
  50. /**
  51. * @param array $baseLocaleData
  52. * @param array $localeData
  53. * @return array
  54. */
  55. protected function comparePhrase($baseLocaleData, $localeData)
  56. {
  57. $missing = array_diff_key($baseLocaleData, $localeData);
  58. $extra = array_diff_key($localeData, $baseLocaleData);
  59. $failures = [];
  60. if (!empty($missing)) {
  61. $failures['missing'] = array_keys($missing);
  62. }
  63. if (!empty($extra)) {
  64. $failures['extra'] = array_keys($extra);
  65. }
  66. return $failures;
  67. }
  68. /**
  69. * @param string[][][] $failures Array errors in format $failures[$locale][$errorType][$message]
  70. * @param string $message
  71. * @return string
  72. */
  73. protected function printMessage($failures, $message = '')
  74. {
  75. $message .= PHP_EOL;
  76. foreach ($failures as $locale => $localeErrors) {
  77. $message .= $locale . PHP_EOL;
  78. foreach ($localeErrors as $typeError => $error) {
  79. $message .= PHP_EOL . "##########" . PHP_EOL . ucfirst($typeError) . ':' . PHP_EOL;
  80. foreach ($error as $phrase) {
  81. $message .= '"' . $phrase . '","' . $phrase . '"' . PHP_EOL;
  82. }
  83. }
  84. }
  85. return $message;
  86. }
  87. }