View source
<?php
namespace Drupal\Tests\node\Functional\Views;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Views;
class BulkFormTest extends NodeTestBase {
protected static $modules = [
'node_test_views',
'language',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_node_bulk_form',
];
protected $nodes;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
ConfigurableLanguage::createFromLangcode('en-gb')
->save();
ConfigurableLanguage::createFromLangcode('it')
->save();
$this->nodes = [];
$langcodes = [
'en',
'en-gb',
'it',
];
for ($i = 1; $i <= 5; $i++) {
$langcode = $langcodes[($i - 1) % 3];
$values = [
'title' => $this
->randomMachineName() . ' [' . $i . ':' . $langcode . ']',
'langcode' => $langcode,
'promote' => FALSE,
];
$node = $this
->drupalCreateNode($values);
$this->nodes[] = $node;
}
for ($i = 0; $i < 2; $i++) {
$node = $this->nodes[$i];
foreach ($langcodes as $langcode) {
if (!$node
->hasTranslation($langcode)) {
$title = $this
->randomMachineName() . ' [' . $node
->id() . ':' . $langcode . ']';
$translation = $node
->addTranslation($langcode, [
'title' => $title,
'promote' => FALSE,
]);
}
}
$node
->save();
}
$node = $this->nodes[2];
$langcode = 'en';
$title = $this
->randomMachineName() . ' [' . $node
->id() . ':' . $langcode . ']';
$translation = $node
->addTranslation($langcode, [
'title' => $title,
]);
$node
->save();
$view = Views::getView('test_node_bulk_form');
$view
->execute();
$this
->assertCount(10, $view->result, 'All created translations are selected.');
$this
->drupalLogin($this
->drupalCreateUser([
'administer nodes',
'access content overview',
'bypass node access',
]));
$this
->drupalGet('test-node-bulk-form');
$elements = $this
->assertSession()
->selectExists('edit-action')
->findAll('css', 'option');
$this
->assertCount(8, $elements, 'All node operations are found.');
}
public function testBulkForm() {
$node = reset($this->nodes);
$this
->assertTrue($node
->isPublished(), 'Node is initially published');
$this
->assertTrue($node
->getTranslation('en-gb')
->isPublished(), 'Node translation is published');
$this
->assertTrue($node
->getTranslation('it')
->isPublished(), 'Node translation is published');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertFalse($node
->isPublished(), 'Node has been unpublished');
$this
->assertTrue($node
->getTranslation('en-gb')
->isPublished(), 'Node translation has not been unpublished');
$this
->assertTrue($node
->getTranslation('it')
->isPublished(), 'Node translation has not been unpublished');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_publish_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertTrue($node
->isPublished(), 'Node has been published again');
$this
->assertFalse($node
->isSticky(), 'Node is not sticky');
$this
->assertFalse($node
->getTranslation('en-gb')
->isSticky(), 'Node translation is not sticky');
$this
->assertFalse($node
->getTranslation('it')
->isSticky(), 'Node translation is not sticky');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_make_sticky_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertTrue($node
->isSticky(), 'Node has been made sticky');
$this
->assertFalse($node
->getTranslation('en-gb')
->isSticky(), 'Node translation has not been made sticky');
$this
->assertFalse($node
->getTranslation('it')
->isSticky(), 'Node translation has not been made sticky');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_make_unsticky_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertFalse($node
->isSticky(), 'Node is not sticky anymore');
$this
->assertFalse($node
->isPromoted(), 'Node is not promoted to the front page');
$this
->assertFalse($node
->getTranslation('en-gb')
->isPromoted(), 'Node translation is not promoted to the front page');
$this
->assertFalse($node
->getTranslation('it')
->isPromoted(), 'Node translation is not promoted to the front page');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_promote_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertTrue($node
->isPromoted(), 'Node has been promoted to the front page');
$this
->assertFalse($node
->getTranslation('en-gb')
->isPromoted(), 'Node translation has not been promoted to the front page');
$this
->assertFalse($node
->getTranslation('it')
->isPromoted(), 'Node translation has not been promoted to the front page');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpromote_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode($node
->id());
$this
->assertFalse($node
->isPromoted(), 'Node has been demoted');
$edit = [
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
'node_bulk_form[2]' => TRUE,
'node_bulk_form[3]' => TRUE,
'node_bulk_form[4]' => TRUE,
'node_bulk_form[5]' => FALSE,
'node_bulk_form[6]' => TRUE,
'node_bulk_form[7]' => FALSE,
'node_bulk_form[8]' => TRUE,
'node_bulk_form[9]' => FALSE,
'action' => 'node_unpublish_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$node = $this
->loadNode(1);
$this
->assertFalse($node
->getTranslation('en')
->isPublished(), '1: English translation has been unpublished');
$this
->assertFalse($node
->getTranslation('en-gb')
->isPublished(), '1: British English translation has been unpublished');
$this
->assertFalse($node
->getTranslation('it')
->isPublished(), '1: Italian translation has been unpublished');
$node = $this
->loadNode(2);
$this
->assertFalse($node
->getTranslation('en')
->isPublished(), '2: English translation has been unpublished');
$this
->assertFalse($node
->getTranslation('en-gb')
->isPublished(), '2: British English translation has been unpublished');
$this
->assertTrue($node
->getTranslation('it')
->isPublished(), '2: Italian translation has not been unpublished');
$node = $this
->loadNode(3);
$this
->assertFalse($node
->getTranslation('en')
->isPublished(), '3: English translation has been unpublished');
$this
->assertTrue($node
->getTranslation('it')
->isPublished(), '3: Italian translation has not been unpublished');
$node = $this
->loadNode(4);
$this
->assertFalse($node
->isPublished(), '4: Node has been unpublished');
$node = $this
->loadNode(5);
$this
->assertTrue($node
->isPublished(), '5: Node has not been unpublished');
}
public function testBulkDeletion() {
$edit = [
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
'node_bulk_form[2]' => TRUE,
'node_bulk_form[3]' => TRUE,
'node_bulk_form[4]' => TRUE,
'node_bulk_form[5]' => FALSE,
'node_bulk_form[6]' => TRUE,
'node_bulk_form[7]' => FALSE,
'node_bulk_form[8]' => TRUE,
'node_bulk_form[9]' => FALSE,
'action' => 'node_delete_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$label = $this
->loadNode(1)
->label();
$this
->assertSession()
->pageTextContains("{$label} (Original translation) - The following content item translations will be deleted:");
$label = $this
->loadNode(2)
->label();
$this
->assertSession()
->pageTextContains("{$label} (Original translation) - The following content item translations will be deleted:");
$label = $this
->loadNode(3)
->getTranslation('en')
->label();
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextNotContains("{$label} (Original translation) - The following content item translations will be deleted:");
$label = $this
->loadNode(4)
->label();
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextNotContains("{$label} (Original translation) - The following content item translations will be deleted:");
$this
->submitForm([], 'Delete');
$node = $this
->loadNode(1);
$this
->assertNull($node, '1: Node has been deleted');
$node = $this
->loadNode(2);
$this
->assertNull($node, '2: Node has been deleted');
$node = $this
->loadNode(3);
$result = count($node
->getTranslationLanguages()) && $node
->language()
->getId() == 'it';
$this
->assertTrue($result, '3: English translation has been deleted');
$node = $this
->loadNode(4);
$this
->assertNull($node, '4: Node has been deleted');
$node = $this
->loadNode(5);
$this
->assertNotEmpty($node, '5: Node has not been deleted');
$this
->assertSession()
->pageTextContains('Deleted 8 content items.');
}
protected function loadNode($id) {
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$storage
->resetCache([
$id,
]);
return $storage
->load($id);
}
}