View source
<?php
namespace Drupal\Tests\views_bulk_edit\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Functional\Views\NodeTestBase;
class ViewsBulkEditActionTest extends NodeTestBase {
use EntityReferenceTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'views_bulk_edit',
'node_test_views',
];
public static $testViews = [
'test_node_bulk_form',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->createContentType([
'type' => 'page',
'name' => 'Page',
]);
$this
->createContentType([
'type' => 'article',
'name' => 'Article',
]);
$admin = $this
->createUser([
'bypass node access',
'access content overview',
'access content',
'administer content types',
'use views bulk edit',
'administer nodes',
]);
$this
->drupalLogin($admin);
}
public function testViewsBulkEdit() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$page1 = $this
->createNode();
$page2 = $this
->createNode();
$article1 = $this
->createNode([
'type' => 'article',
]);
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm([
'action' => 'node_edit_action',
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
], 'Apply to selected items');
$random_title = $this
->randomMachineName();
$this
->assertSession()
->fieldNotExists('node[page][_field_selector][revision_log]');
$this
->submitForm([
'node[page][_field_selector][title]' => '1',
'node[page][title][0][value]' => $random_title,
], 'Confirm');
$storage
->resetCache();
$nodes = array_values($storage
->loadMultiple([
$page1
->id(),
$page2
->id(),
$article1
->id(),
]));
$this
->assertEquals($random_title, $nodes[0]
->getTitle());
$this
->assertEquals($random_title, $nodes[1]
->getTitle());
$this
->assertNotEquals($random_title, $nodes[2]
->getTitle());
}
public function testBulkEditMultipleBundles() {
$page1 = $this
->createNode();
$article1 = $this
->createNode([
'type' => 'article',
]);
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm([
'action' => 'node_edit_action',
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
], 'Apply to selected items');
$random_title = $this
->randomMachineName();
$this
->submitForm([
'node[page][_field_selector][title]' => '1',
'node[page][title][0][value]' => $random_title,
'node[article][_field_selector][title]' => '1',
'node[article][title][0][value]' => $random_title,
], 'Confirm');
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$storage
->resetCache();
$nodes = array_values($storage
->loadMultiple([
$page1
->id(),
$article1
->id(),
]));
$this
->assertEquals($random_title, $nodes[0]
->getTitle());
$this
->assertEquals($random_title, $nodes[1]
->getTitle());
}
public function testOnlySelectedValuesAreChanged() {
$page1 = $this
->createNode();
$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][title][0][value]' => $random_title,
], 'Confirm');
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$storage
->resetCache();
$this
->assertNotEquals($random_title, $storage
->load($page1
->id())
->getTitle());
}
public function testRevisionUi() {
$page1 = $this
->createNode();
$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();
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$latestes_revision = $page1
->getRevisionId();
$this
->submitForm([
'node[page][_field_selector][title]' => '1',
'node[page][title][0][value]' => $random_title,
'node[page][revision_information][revision]' => FALSE,
], 'Confirm');
$storage
->resetCache();
$page1 = $storage
->load($page1
->id());
$this
->assertEquals($random_title, $page1
->getTitle());
$this
->assertEquals($latestes_revision, $page1
->getRevisionId());
$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][title][0][value]' => $random_title,
'node[page][revision_information][revision]' => TRUE,
'node[page][revision_information][revision_log]' => 'My new revision',
], 'Confirm');
$storage
->resetCache();
$page1 = $storage
->load($page1
->id());
$this
->assertEquals($random_title, $page1
->getTitle());
$this
->assertNotEquals($latestes_revision, $page1
->getRevisionId());
$this
->assertEquals('My new revision', $page1
->getRevisionLogMessage());
}
public function testFieldsNotDisplayedAreIgnored() {
EntityFormMode::create([
'id' => 'node.bulk_edit',
'label' => 'Bulk Edit',
'targetEntityType' => 'node',
])
->save();
$display = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'page',
'mode' => 'bulk_edit',
'status' => TRUE,
]);
$this
->createNode();
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm([
'action' => 'node_edit_action',
'node_bulk_form[0]' => TRUE,
], 'Apply to selected items');
$this
->assertSession()
->fieldExists('node[page][_field_selector][title]');
$display
->removeComponent('title')
->save();
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm([
'action' => 'node_edit_action',
'node_bulk_form[0]' => TRUE,
], 'Apply to selected items');
$this
->assertSession()
->fieldNotExists('node[page][_field_selector][title]');
}
public function testChangeMethods() {
$handler_settings = [
'target_bundles' => [
'article' => 'article',
],
];
$this
->createEntityReferenceField('node', 'page', 'field_reference', 'Reference', 'node', 'default', $handler_settings, FieldStorageConfigInterface::CARDINALITY_UNLIMITED);
\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());
}
}