InlineParserTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model;
  7. class InlineParserTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Translation\Model\Inline\Parser
  11. */
  12. protected $_inlineParser;
  13. /** @var string */
  14. protected $_storeId = 'default';
  15. protected function setUp()
  16. {
  17. /** @var $inline \Magento\Framework\Translate\Inline */
  18. $inline = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  19. ->create(\Magento\Framework\Translate\Inline::class);
  20. $this->_inlineParser = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  21. \Magento\Translation\Model\Inline\Parser::class,
  22. ['translateInline' => $inline]
  23. );
  24. /* Called getConfig as workaround for setConfig bug */
  25. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  26. \Magento\Store\Model\StoreManagerInterface::class
  27. )->getStore(
  28. $this->_storeId
  29. )->getConfig(
  30. 'dev/translate_inline/active'
  31. );
  32. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  33. \Magento\Framework\App\Config\MutableScopeConfigInterface::class
  34. )->setValue(
  35. 'dev/translate_inline/active',
  36. true,
  37. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  38. $this->_storeId
  39. );
  40. }
  41. /**
  42. * @dataProvider processAjaxPostDataProvider
  43. */
  44. public function testProcessAjaxPost($originalText, $translatedText, $isPerStore = null)
  45. {
  46. $inputArray = [['original' => $originalText, 'custom' => $translatedText]];
  47. if ($isPerStore !== null) {
  48. $inputArray[0]['perstore'] = $isPerStore;
  49. }
  50. $this->_inlineParser->processAjaxPost($inputArray);
  51. $model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  52. \Magento\Translation\Model\StringUtils::class
  53. );
  54. $model->load($originalText);
  55. try {
  56. $this->assertEquals($translatedText, $model->getTranslate());
  57. $model->delete();
  58. } catch (\Exception $e) {
  59. $model->delete();
  60. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  61. ->get(\Psr\Log\LoggerInterface::class)
  62. ->critical($e);
  63. }
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function processAjaxPostDataProvider()
  69. {
  70. return [
  71. ['original text 1', 'translated text 1'],
  72. ['original text 2', 'translated text 2', true]
  73. ];
  74. }
  75. public function testSetGetIsJson()
  76. {
  77. $isJsonProperty = new \ReflectionProperty(get_class($this->_inlineParser), '_isJson');
  78. $isJsonProperty->setAccessible(true);
  79. $this->assertFalse($isJsonProperty->getValue($this->_inlineParser));
  80. $setIsJsonMethod = new \ReflectionMethod($this->_inlineParser, 'setIsJson');
  81. $setIsJsonMethod->setAccessible(true);
  82. $setIsJsonMethod->invoke($this->_inlineParser, true);
  83. $this->assertTrue($isJsonProperty->getValue($this->_inlineParser));
  84. }
  85. }