Commit 2b8dff9a authored by Jahanzaib Chishti's avatar Jahanzaib Chishti Committed by Christian López Espínola
Browse files

Issue #3319591 by jahanzaib_at_straker, penyaskito: When using "Download as...

Issue #3319591 by jahanzaib_at_straker, penyaskito: When using "Download as Unpublished" option in Lingotek module, Paragraphs remain unpublished when parent node is published
parent be7310c8
Loading
Loading
Loading
Loading
+3
Changes for src/LingotekContentTranslationService.php: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1404,6 +1404,9 @@ class LingotekContentTranslationService implements LingotekContentTranslationSer
          $published_value = ($published_setting === 'published') ? NodeInterface::PUBLISHED : NodeInterface::NOT_PUBLISHED;
          $translation->set($published_field, $published_value);
        }
        if ($entity->getEntityTypeId() === 'paragraph') {
          $translation->set($published_field, $revision->get($revision->getEntityType()->getKey('published'))->value);
        }
      }

      // If there is any content moderation module is enabled, we may need to
+123
Changes for tests/src/Functional/LingotekNodeParagraphsTranslationTest.php: 123 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1061,6 +1061,129 @@ class LingotekNodeParagraphsTranslationTest extends LingotekTestBase {
    $this->assertText('Dogs are cool for the second time');
  }

  /**
   * Tests that a paragraph is published when its parent node has been published.
   */
  public function testStatusChangeNodeWithParagraphsTranslation() {
    $this->saveLingotekContentTranslationSettings([
      'node' => [
        'paragraphed_content_demo' => [
          'profiles' => 'automatic',
          'fields' => [
            'title' => 1,
            'field_paragraphs_demo' => 1,
          ],
          'moderation' => [
            'upload_status' => 'published',
            'download_transition' => 'create_new_draft',
          ],
        ],
      ],
      'paragraph' => [
        'image_text' => [
          'fields' => [
            'field_image_demo' => ['title', 'alt'],
            'field_text_demo' => 1,
          ],
        ],
      ],
    ]);
    // Login as admin.
    $this->drupalLogin($this->rootUser);

    // Set the translation download status as "unpublished".
    $assert_session = $this->assertSession();
    $this->drupalGet('admin/lingotek/settings');
    $edit = ['target_download_status' => 'unpublished'];
    $this->submitForm($edit, 'Save', 'lingoteksettings-tab-preferences-form');

    // Assert the settings are saved successfully.
    $assert_session->fieldValueEquals('edit-target-download-status', 'unpublished');

    // Add paragraphed content.
    $this->drupalGet('node/add/paragraphed_content_demo');
    $this->drupalPostForm(NULL, NULL, t('Add Image + Text'));
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['field_paragraphs_demo[0][subform][field_text_demo][0][value]'] = 'Llamas are very cool';
    $edit['moderation_state[0][state]'] = 'published';
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->node = Node::load(1);

    // Check that only the configured fields have been uploaded.
    $data = json_decode(\Drupal::state()->get('lingotek.uploaded_content', '[]'), TRUE);
    $this->verbose(var_export($data, TRUE));
    $this->assertUploadedDataFieldCount($data, 2);
    $this->assertEqual($data['title'][0]['value'], 'Llamas are cool');
    $this->assertEqual($data['field_paragraphs_demo'][0]['field_text_demo'][0]['value'], 'Llamas are very cool');

    // Check that the url used was the right one.
    $uploaded_url = \Drupal::state()->get('lingotek.uploaded_url');
    $this->assertIdentical(\Drupal::request()->getUriForPath('/node/1'), $uploaded_url, 'The node url was used.');

    // Check that the profile used was the right one.
    $used_profile = \Drupal::state()->get('lingotek.used_profile');
    $this->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');

    // Check that the content is published.
    $this->drupalGet('node/1');
    $this->clickLink('Edit');
    $publish_checkbox = $this->xpath('//option[@value="published" and @selected="selected"]');
    $this->assertEquals(1, count($publish_checkbox));

    // The document should have been automatically uploaded, so let's check
    // the upload status.
    $this->clickLink('Translate');
    $this->clickLink('Check Upload Status');
    $this->assertText('The import for node Llamas are cool is complete.');

    // Request translation.
    $link = $this->xpath('//a[normalize-space()="Request translation" and contains(@href,"es_AR")]');
    $link[0]->click();
    $this->assertText("Locale 'es_AR' was added as a translation target for node Llamas are cool.");

    // Check translation status.
    $this->clickLink('Check translation status');
    $this->assertText('The es_AR translation for node Llamas are cool is ready for download.');

    // Check that the Edit link points to the workbench and it is opened in a new tab.
    $this->assertLingotekWorkbenchLink('es_AR', 'dummy-document-hash-id', 'Edit in Lingotek Workbench');

    // Download completed translation.
    $this->clickLink('Translate');
    $this->clickLink('Download completed translation');
    $this->assertText('The translation of node Llamas are cool into es_AR has been downloaded.');

    // The source should stay published, but the new translation is not.
    $this->assertText('Not published');
    $this->assertText('Published');

    // The last revision for the translation has the changed texts.
    $this->clickLink('Las llamas son chulas');
    $this->clickLink('Latest version');

    $this->assertText('Las llamas son chulas');
    $this->assertText('Las llamas son muy chulas');

    // We check this is a draft and publish the translation.
    $this->clickLink('Edit');
    $draft_option = $this->xpath('//option[@value="draft" and @selected="selected"]');
    $this->assertEquals(1, count($draft_option));

    $edit = ['moderation_state[0][state]' => 'published'];
    $this->drupalPostForm(NULL, $edit, 'Save (this translation)');

    $this->clickLink('Edit');
    $publish_option = $this->xpath('//option[@value="published" and @selected="selected"]');
    $this->assertEquals(1, count($publish_option));

    // Check no paragraph is unpublished after publishing the content.
    $this->clickLink('View');
    $unpublished_article = $this->xpath("//*[contains(@class, 'paragraph--unpublished')]");
    $this->assertEquals(0, count($unpublished_article));
  }

  protected function setParagraphFieldsTranslatability(): void {
    $edit = [];
    $edit['settings[node][paragraphed_content_demo][fields][field_paragraphs_demo]'] = 1;