function EntityDecoupledTranslationRevisionsTest::doEditStep

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php \Drupal\KernelTests\Core\Entity\EntityDecoupledTranslationRevisionsTest::doEditStep()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php \Drupal\KernelTests\Core\Entity\EntityDecoupledTranslationRevisionsTest::doEditStep()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php \Drupal\KernelTests\Core\Entity\EntityDecoupledTranslationRevisionsTest::doEditStep()

Saves a new revision of the test entity.

Parameters

string $active_langcode: The language of the translation for which a new revision will be saved.

bool $default_revision: Whether the revision should be flagged as the default revision.

bool $untranslatable_update: (optional) Whether an untranslatable field update should be performed. Defaults to FALSE.

bool $valid: (optional) Whether entity validation is expected to succeed. Defaults to TRUE.

Return value

int The new revision identifier.

Throws

\Drupal\Core\Entity\EntityStorageException

File

core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php, line 319

Class

EntityDecoupledTranslationRevisionsTest
Test decoupled translation revisions.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doEditStep($active_langcode, $default_revision, $untranslatable_update = FALSE, $valid = TRUE) {
    $this->stepInfo = [
        $active_langcode,
        $default_revision,
        $untranslatable_update,
        $valid,
    ];
    // If changes to untranslatable fields affect only the default translation,
    // we can different values for untranslatable fields in the various
    // revision translations, so we need to track their previous value per
    // language.
    $all_translations_affected = !$this->state
        ->get('entity_test.untranslatable_fields.default_translation_affected');
    $previous_untranslatable_field_langcode = $all_translations_affected ? LanguageInterface::LANGCODE_DEFAULT : $active_langcode;
    // Initialize previous data tracking.
    if (!isset($this->translations)) {
        $this->translations[$active_langcode] = EntityTestMulRev::create();
        $this->previousRevisionId[$active_langcode] = 0;
        $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode] = NULL;
    }
    if (!isset($this->translations[$active_langcode])) {
        $this->translations[$active_langcode] = reset($this->translations)
            ->addTranslation($active_langcode);
        $this->previousRevisionId[$active_langcode] = 0;
        $this->previousUntranslatableFieldValue[$active_langcode] = NULL;
    }
    // We want to update previous data only if we expect a valid result,
    // otherwise we would be just polluting it with invalid values.
    if ($valid) {
        $entity =& $this->translations[$active_langcode];
        $previous_revision_id =& $this->previousRevisionId[$active_langcode];
        $previous_untranslatable_field_value =& $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode];
    }
    else {
        $entity = clone $this->translations[$active_langcode];
        $previous_revision_id = $this->previousRevisionId[$active_langcode];
        $previous_untranslatable_field_value = $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode];
    }
    // Check that after instantiating a new revision for the specified
    // translation, we are resuming work from where we left the last time. If
    // that is the case, the label generated for the previous revision should
    // match the stored one.
    if (!$entity->isNew()) {
        $previous_label = NULL;
        if (!$entity->isNewTranslation()) {
            $previous_label = $this->generateNewEntityLabel($entity, $previous_revision_id);
            $latest_affected_revision_id = $this->storage
                ->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()
                ->getId());
        }
        else {
            // Normally it would make sense to load the default revision in this
            // case, however that would mean simulating here the logic that we need
            // to test, thus "masking" possible flaws. To avoid that, we simply
            // pretend we are starting from an earlier non translated revision.
            // This ensures that the we can check that the merging logic is applied
            // also when adding a new translation.
            $latest_affected_revision_id = 1;
        }
        $previous_revision_id = (int) $entity->getLoadedRevisionId();
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_affected_revision */
        $latest_affected_revision = $this->storage
            ->loadRevision($latest_affected_revision_id);
        $translation = $latest_affected_revision->hasTranslation($active_langcode) ? $latest_affected_revision->getTranslation($active_langcode) : $latest_affected_revision->addTranslation($active_langcode);
        $entity = $this->storage
            ->createRevision($translation, $default_revision);
        $this->assertEquals($default_revision, $entity->isDefaultRevision());
        $this->assertEquals($translation->getLoadedRevisionId(), $entity->getLoadedRevisionId());
        $this->assertEquals($previous_label, $entity->label(), $this->formatMessage('Loaded translatable field value does not match the previous one.'));
    }
    // Check that the previous untranslatable field value is loaded in the new
    // revision as expected. When we are dealing with a non default translation
    // the expected value is always the one stored in the default revision, as
    // untranslatable fields can only be changed in the default translation or
    // in the default revision, depending on the configured mode.
    $value = $entity->get('non_mul_field')->value;
    if (isset($previous_untranslatable_field_value)) {
        $this->assertEquals($previous_untranslatable_field_value, $value, $this->formatMessage('Loaded untranslatable field value does not match the previous one.'));
    }
    elseif (!$entity->isDefaultTranslation()) {
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $default_revision */
        $default_revision = $this->storage
            ->loadUnchanged($entity->id());
        $expected_value = $default_revision->get('non_mul_field')->value;
        $this->assertEquals($expected_value, $value, $this->formatMessage('Loaded untranslatable field value does not match the previous one.'));
    }
    // Perform a change and store it.
    $label = $this->generateNewEntityLabel($entity, $previous_revision_id, TRUE);
    $entity->set('name', $label);
    if ($untranslatable_update) {
        // Store the revision ID of the previous untranslatable fields update in
        // the new value, besides the upcoming revision ID. Useful to analyze test
        // failures.
        $prev = 0;
        if (isset($previous_untranslatable_field_value)) {
            preg_match('/^\\d+ -> (\\d+)$/', $previous_untranslatable_field_value, $matches);
            $prev = $matches[1];
        }
        $value = $prev . ' -> ' . ($entity->getLoadedRevisionId() + 1);
        $entity->set('non_mul_field', $value);
        $previous_untranslatable_field_value = $value;
    }
    $violations = $entity->validate();
    $messages = [];
    foreach ($violations as $violation) {
        
        /** \Symfony\Component\Validator\ConstraintViolationInterface */
        $messages[] = $violation->getMessage();
    }
    $this->assertEquals($valid, !$violations->count(), $this->formatMessage('Validation does not match the expected result: %s', implode(', ', $messages)));
    if ($valid) {
        $entity->save();
        // Reload the current revision translation and the default revision to
        // make sure data was stored correctly.
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
        $entity = $this->storage
            ->loadRevision($entity->getRevisionId());
        $entity = $entity->getTranslation($active_langcode);
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $default_entity */
        $default_entity = $this->storage
            ->loadUnchanged($entity->id());
        // Verify that the values for the current revision translation match the
        // expected ones, while for the other translations they match the default
        // revision. We also need to verify that only the current revision
        // translation was marked as affected.
        foreach ($entity->getTranslationLanguages() as $langcode => $language) {
            $translation = $entity->getTranslation($langcode);
            $rta_expected = $langcode == $active_langcode || $untranslatable_update && $all_translations_affected;
            $this->assertEquals($rta_expected, $translation->isRevisionTranslationAffected(), $this->formatMessage("'{$langcode}' translation incorrectly affected"));
            $label_expected = $label;
            if ($langcode !== $active_langcode) {
                $default_translation = $default_entity->hasTranslation($langcode) ? $default_entity->getTranslation($langcode) : $default_entity;
                $label_expected = $default_translation->label();
            }
            $this->assertEquals($label_expected, $translation->label(), $this->formatMessage("Incorrect '{$langcode}' translation label"));
        }
    }
    return $entity->getRevisionId();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.