View source  
  <?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\BrowserTestBase;
class LocaleExportTest extends BrowserTestBase {
  
  protected static $modules = [
    'locale',
  ];
  
  protected $defaultTheme = 'stark';
  
  protected $adminUser = NULL;
  
  protected function setUp() : void {
    parent::setUp();
    $this->adminUser = $this
      ->drupalCreateUser([
      'administer languages',
      'translate interface',
      'access administration pages',
    ]);
    $this
      ->drupalLogin($this->adminUser);
    
    \Drupal::service('file_system')
      ->copy(__DIR__ . '/../../../tests/test.de.po', 'translations://', FileSystemInterface::EXISTS_REPLACE);
    \Drupal::service('file_system')
      ->copy(__DIR__ . '/../../../tests/test.xx.po', 'translations://', FileSystemInterface::EXISTS_REPLACE);
  }
  
  public function testExportTranslation() {
    $file_system = \Drupal::service('file_system');
    
    $name = $file_system
      ->tempnam('temporary://', "po_") . '.po';
    file_put_contents($name, $this
      ->getPoFile());
    $this
      ->drupalGet('admin/config/regional/translate/import');
    $this
      ->submitForm([
      'langcode' => 'fr',
      'files[file]' => $name,
    ], 'Import');
    $file_system
      ->unlink($name);
    
    $this
      ->drupalGet('admin/config/regional/translate/export');
    $this
      ->submitForm([
      'langcode' => 'fr',
    ], 'Export');
    
    $this
      ->assertSession()
      ->pageTextContains('# French translation of Drupal');
    
    $this
      ->assertSession()
      ->pageTextContains('msgstr "lundi"');
    
    $name = $file_system
      ->tempnam('temporary://', "po2_") . '.po';
    file_put_contents($name, $this
      ->getCustomPoFile());
    $this
      ->drupalGet('admin/config/regional/translate/import');
    $this
      ->submitForm([
      'langcode' => 'fr',
      'files[file]' => $name,
      'customized' => 1,
    ], 'Import');
    $file_system
      ->unlink($name);
    
    $this->container
      ->get('locale.storage')
      ->createString()
      ->setString('February')
      ->save();
    
    $this
      ->drupalGet('admin/config/regional/translate/export');
    $this
      ->submitForm([
      'langcode' => 'fr',
      'content_options[not_customized]' => FALSE,
      'content_options[customized]' => TRUE,
      'content_options[not_translated]' => FALSE,
    ], 'Export');
    
    $this
      ->assertSession()
      ->pageTextContains('# French translation of Drupal');
    
    $this
      ->assertSession()
      ->pageTextContains('msgstr "janvier"');
    
    $this
      ->assertSession()
      ->responseNotContains('msgid "February"');
    
    $this
      ->drupalGet('admin/config/regional/translate/export');
    $this
      ->submitForm([
      'langcode' => 'fr',
      'content_options[not_customized]' => FALSE,
      'content_options[customized]' => FALSE,
      'content_options[not_translated]' => TRUE,
    ], 'Export');
    
    $this
      ->assertSession()
      ->pageTextContains('# French translation of Drupal');
    
    $this
      ->assertSession()
      ->responseNotContains('msgstr "janvier"');
    
    $this
      ->assertSession()
      ->responseContains($this
      ->getUntranslatedString());
  }
  
  public function testExportTranslationTemplateFile() {
    
    $this
      ->drupalGet('admin/config/regional/language');
    
    $this
      ->drupalGet('admin/config/regional/translate/export');
    $this
      ->submitForm([], 'Export');
    
    $this
      ->assertSession()
      ->pageTextContains('# LANGUAGE translation of PROJECT');
  }
  
  public function getPoFile() {
    return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "Monday"
msgstr "lundi"
EOF;
  }
  
  public function getCustomPoFile() {
    return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "January"
msgstr "janvier"
EOF;
  }
  
  public function getUntranslatedString() {
    return <<<EOF
msgid "February"
msgstr ""
EOF;
  }
}