You are here

public function ViewsBulkEditActionTest::testChangeMethods in Views Bulk Edit 8.2

Test the change methods.

File

tests/src/Functional/ViewsBulkEditActionTest.php, line 242

Class

ViewsBulkEditActionTest
Tests the core edit action.

Namespace

Drupal\Tests\views_bulk_edit\Functional

Code

public function testChangeMethods() {
  $handler_settings = [
    'target_bundles' => [
      'article' => 'article',
    ],
  ];
  $this
    ->createEntityReferenceField('node', 'page', 'field_reference', 'Reference', 'node', 'default', $handler_settings, FieldStorageConfigInterface::CARDINALITY_UNLIMITED);

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $fd */
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', 'page')
    ->setComponent('field_reference', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $page1 = $this
    ->createNode();
  $article1 = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Foo',
  ]);
  $article2 = $this
    ->createNode([
    'type' => 'article',
    'title' => 'Bar',
  ]);
  $this
    ->drupalGet('test-node-bulk-form');
  $this
    ->submitForm([
    'action' => 'node_edit_action',
    'node_bulk_form[0]' => TRUE,
  ], 'Apply to selected items');
  $random_title = $this
    ->randomMachineName();
  $this
    ->submitForm([
    'node[page][_field_selector][title]' => '1',
    'node[page][_field_selector][field_reference]' => '1',
    'node[page][title_change_method]' => 'replace',
    'node[page][field_reference_change_method]' => 'replace',
    'node[page][title][0][value]' => $random_title,
    'node[page][field_reference][0][target_id]' => $article1
      ->label(),
  ], 'Confirm');
  $storage
    ->resetCache();
  $page1 = $storage
    ->load($page1
    ->id());
  $this
    ->assertEquals($random_title, $page1
    ->getTitle());
  $this
    ->assertEquals([
    [
      'target_id' => $article1
        ->id(),
    ],
  ], $page1->field_reference
    ->getValue());
  $page_title = $page1
    ->getTitle();
  $this
    ->drupalGet('test-node-bulk-form');
  $this
    ->submitForm([
    'action' => 'node_edit_action',
    'node_bulk_form[0]' => TRUE,
  ], 'Apply to selected items');
  $random_title = $this
    ->randomMachineName();
  $this
    ->submitForm([
    'node[page][_field_selector][title]' => '1',
    'node[page][_field_selector][field_reference]' => '1',
    'node[page][title_change_method]' => 'append',
    'node[page][field_reference_change_method]' => 'new',
    'node[page][title][0][value]' => $random_title,
    'node[page][field_reference][0][target_id]' => $article2
      ->label(),
  ], 'Confirm');
  $storage
    ->resetCache();
  $page1 = $storage
    ->load($page1
    ->id());
  $this
    ->assertEquals($page_title . ' ' . $random_title, $page1
    ->getTitle());
  $this
    ->assertEquals([
    [
      'target_id' => $article1
        ->id(),
    ],
    [
      'target_id' => $article2
        ->id(),
    ],
  ], $page1->field_reference
    ->getValue());
}