Error message

  • Warning: count(): Parameter must be an array or an object that implements Countable in _api_make_match_member_link() (line 1230 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Warning: count(): Parameter must be an array or an object that implements Countable in _api_make_match_member_link() (line 1230 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).

class ContentEntitySourceTranslatableEntityTest

Tests always embedded entity reference fields.

@group tmgmt

Hierarchy

Expanded class hierarchy of ContentEntitySourceTranslatableEntityTest

File

sources/content/tests/src/Functional/ContentEntitySourceTranslatableEntityTest.php, line 17

Namespace

Drupal\Tests\tmgmt_content\Functional
View source
class ContentEntitySourceTranslatableEntityTest extends TMGMTTestBase {
  use TmgmtEntityTestTrait;
  use EntityReferenceTestTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = array(
    'node',
    'field',
    'tmgmt_composite_test',
    'tmgmt_content',
  );

  /**
   * {@inheritdoc}
   */
  function setUp() : void {
    parent::setUp();
    $this
      ->addLanguage('de');
    $this
      ->loginAsAdmin([
      'administer tmgmt',
    ]);

    // Create article content type.
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

    // Enable entity translations for entity_test_composite and node.
    $content_translation_manager = \Drupal::service('content_translation.manager');
    $content_translation_manager
      ->setEnabled('entity_test_composite', 'entity_test_composite', TRUE);
    $content_translation_manager
      ->setEnabled('node', 'article', TRUE);
  }

  /**
   * Tests that the referenced entities are always embedded.
   */
  public function testTranslatableEntityReferences() {

    // Assert there is NO embedded references yet.
    $this
      ->drupalGet('/admin/tmgmt/settings');
    $xpath = '//*[@id="edit-content"]';
    $embedded_entity = '<label for="edit-always-embedded">Always embedded</label>';
    $embedded_node = '<span class="fieldset-legend">Content</span>';
    $this
      ->assertSession()
      ->pageTextNotContains('Authored by (User)');
    $this
      ->assertStringNotContainsString($embedded_entity, $this
      ->xpath($xpath)[0]
      ->getOuterHtml());
    $this
      ->assertStringNotContainsString($embedded_node, $this
      ->xpath($xpath)[0]
      ->getOuterHtml());

    // Create the reference field to the composite entity test.
    $this
      ->createEntityReferenceField('node', 'article', 'entity_test_composite', 'entity_test_composite', 'entity_test_composite');
    FieldConfig::loadByName('node', 'article', 'entity_test_composite')
      ->setTranslatable(FALSE)
      ->save();

    // Assert there IS the entity_test_composite as entity embedded now.
    $this
      ->drupalGet('/admin/tmgmt/settings');
    $this
      ->assertSession()
      ->pageTextContains('Content: entity_test_composite');
    $this
      ->assertStringContainsString($embedded_entity, $this
      ->xpath($xpath)[0]
      ->getOuterHtml());

    // Create the composite entity test.
    $composite = EntityTestComposite::create(array(
      'name' => 'composite name',
    ));
    $composite
      ->save();

    // Create a node with a reference to the composite entity test.
    $node = $this
      ->createNode(array(
      'title' => 'node title',
      'type' => 'article',
      'entity_test_composite' => $composite,
    ));

    // Create a job and job item for the node.
    $job = $this
      ->createJob();
    $job
      ->save();
    $job_item = tmgmt_job_item_create('content', $node
      ->getEntityTypeId(), $node
      ->id(), [
      'tjid' => $job
        ->id(),
    ]);
    $job_item
      ->save();

    // Get the data and check it contains the data for the composite entity.
    $data = $job_item
      ->getData();
    $this
      ->assertTrue(isset($data['entity_test_composite']));
    $this
      ->assertEquals('entity_test_composite', $data['entity_test_composite']['#label']);
    $this
      ->assertFalse(isset($data['entity_test_composite'][0]['#label']));
    $this
      ->assertEquals('Name', $data['entity_test_composite'][0]['entity']['name']['#label']);
    $this
      ->assertEquals('composite name', $data['entity_test_composite'][0]['entity']['name'][0]['value']['#text']);

    // Ensure that only Content is shown in the source select form.
    $this
      ->drupalGet('/admin/tmgmt/sources');
    $this
      ->assertSession()
      ->optionExists('edit-source', 'content:node');
    $this
      ->assertSession()
      ->optionNotExists('edit-source', 'content:entity_test_composite');

    // Now request a translation and save it back.
    $job->translator = $this->default_translator
      ->id();
    $job
      ->requestTranslation();
    $items = $job
      ->getItems();
    $item = reset($items);
    $item
      ->acceptTranslation();

    // Load existing node and test translating
    $node = Node::load($node
      ->id());
    $translation = $node
      ->getTranslation('de');

    // The IDs of the composite entities did not change.
    $this
      ->assertEquals($node
      ->get('entity_test_composite')->target_id, $translation
      ->get('entity_test_composite')->target_id);
    $composite = EntityTestComposite::load($translation
      ->get('entity_test_composite')->target_id);
    $composite = $composite
      ->getTranslation('de');
    $this
      ->assertEquals('de(de-ch): composite name', $composite
      ->label());
  }

  /**
   * Tests UI of the embedded entities with untranslatable target types.
   */
  public function testTranslatableEntityReferencesUntranslatableTarget() {

    // Disable entity translations for entity_test_composite.
    \Drupal::service('content_translation.manager')
      ->setEnabled('entity_test_composite', 'entity_test_composite', FALSE);

    // Create the translatable reference field to the composite entity test.
    $this
      ->createEntityReferenceField('node', 'article', 'entity_test_t_composite', 'entity_test_t_composite', 'entity_test_composite');

    // Assert there is the entity_test_composite as entity embedded now.
    $this
      ->drupalGet('/admin/tmgmt/settings');
    $this
      ->assertSession()
      ->fieldExists('embedded_fields[node][entity_test_t_composite]');
    $this
      ->assertSession()
      ->pageTextContains('Note: This is a translatable field to an untranslatable target. A copy of the target will be created when translating.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntitySourceTranslatableEntityTest::$modules protected static property Modules to enable. Overrides TMGMTTestBase::$modules
ContentEntitySourceTranslatableEntityTest::setUp function Overrides DrupalWebTestCase::setUp() Overrides TMGMTTestBase::setUp
ContentEntitySourceTranslatableEntityTest::testTranslatableEntityReferences public function Tests that the referenced entities are always embedded.
ContentEntitySourceTranslatableEntityTest::testTranslatableEntityReferencesUntranslatableTarget public function Tests UI of the embedded entities with untranslatable target types.
TmgmtEntityTestTrait::$field_names public property
TmgmtEntityTestTrait::attachFields function Creates fields of type text and text_with_summary of different cardinality.
TmgmtEntityTestTrait::createNodeType function Creates node type with several text fields with different cardinality.
TmgmtEntityTestTrait::createTaxonomyTerm function Creates a taxonomy term of a given vocabulary.
TmgmtEntityTestTrait::createTaxonomyVocab function Creates taxonomy vocabulary with custom fields.
TmgmtEntityTestTrait::createTranslatableNode protected function Creates a node of a given bundle.
TMGMTTestBase::$defaultTheme protected property
TMGMTTestBase::$default_translator protected property A default translator using the test translator.
TmgmtTestTrait::$admin_permissions protected property List of permissions used by loginAsAdmin().
TmgmtTestTrait::$admin_user protected property Drupal user object created by loginAsAdmin().
TmgmtTestTrait::$languageWeight protected property The language weight for new languages.
TmgmtTestTrait::$translator_permissions protected property List of permissions used by loginAsTranslator().
TmgmtTestTrait::$translator_user protected property Drupal user object created by loginAsTranslator().
TmgmtTestTrait::addLanguage function Sets the proper environment.
TmgmtTestTrait::assertJobItemLangCodes function Asserts job item language codes.
TmgmtTestTrait::assertTextByXpath protected function Asserts text in the page with an xpath expression.
TmgmtTestTrait::clickLinkWithImageTitle function Clicks on an image link with the provided title attribute.
TmgmtTestTrait::createJob function Creates, saves and returns a translation job.
TmgmtTestTrait::createTranslator function Creates, saves and returns a translator.
TmgmtTestTrait::loginAsAdmin function Will create a user with admin permissions and log it in.
TmgmtTestTrait::loginAsTranslator function Will create a user with translator permissions and log it in.