outline-8.x-1.x-dev/tests/src/Kernel/OutlineCrudTest.php
tests/src/Kernel/OutlineCrudTest.php
<?php
namespace Drupal\Tests\outline\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\outline\Entity\Outline;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\outline\Traits\OutlineTestTrait;
/**
* Tests loading, saving and deleting outlines.
*
* @group outline
*/
class OutlineCrudTest extends KernelTestBase {
use OutlineTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'filter',
'system',
'outline',
'outline_crud',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('user', ['users_data']);
$this->installEntitySchema('outline_entry');
}
/**
* Test deleting a outline that contains entries.
*/
public function testOutlineOutlineDeleteWithEntrys() {
$outline = $this->createOutline();
$query = \Drupal::entityQuery('outline_entry')->count();
// Assert that there are no entries left.
$this->assertEquals(0, $query->execute());
$entries = [];
for ($i = 0; $i < 5; $i++) {
$entries[$i] = $this->createEntry($outline);
}
// Set up hierarchy. entry 2 is a child of 1 and 4 a child of 1 and 2.
$entries[2]->parent = [$entries[1]->id()];
$entries[2]->save();
$entries[4]->parent = [$entries[1]->id(), $entries[2]->id()];
$entries[4]->save();
// Assert that there are now 5 entries.
$this->assertEquals(5, $query->execute());
$outline->delete();
// Assert that there are no entries left.
$this->assertEquals(0, $query->execute());
}
/**
* Tests for loading multiple outlines.
*
* @throws \Exception
*/
public function testOutlineOutlineLoadMultiple() {
// Ensure there are no outlines.
$this->assertEmpty(Outline::loadMultiple());
// Create some outlines and assign weights.
$outline1 = $this->createOutline();
$outline1->set('weight', 0);
$outline1->save();
$outline2 = $this->createOutline();
$outline2->set('weight', 1);
$outline2->save();
$outline3 = $this->createOutline();
$outline3->set('weight', 2);
$outline3->save();
// Check if third party settings exist.
$this->assertEquals('bar', $outline1->getThirdPartySetting('outline_crud', 'foo'));
$this->assertEquals('bar', $outline2->getThirdPartySetting('outline_crud', 'foo'));
$this->assertEquals('bar', $outline3->getThirdPartySetting('outline_crud', 'foo'));
// Fetch the names for all outlines, confirm that they are keyed by
// machine name.
$names = outline_get_names();
$this->assertEquals($outline1->id(), $names[$outline1->id()]);
// Fetch the outlines with Outline::loadMultiple(), specifying IDs.
// Ensure they are returned in the same order as the original array.
$outlines = Outline::loadMultiple([
$outline3->id(),
$outline2->id(),
$outline1->id(),
]);
$loaded_order = array_keys($outlines);
$expected_order = [
$outline3->id(),
$outline2->id(),
$outline1->id(),
];
$this->assertSame($expected_order, $loaded_order);
// Test loading outlines by their properties.
$storage = $this->container->get('entity_type.manager')->getStorage('outline');
// Fetch outline 1 by name.
$outline = current($storage->loadByProperties(['name' => $outline1->label()]));
$this->assertEquals($outline1->id(), $outline->id());
// Fetch outline 2 by name and ID.
$outline = current($storage->loadByProperties([
'name' => $outline2->label(),
'oid' => $outline2->id(),
]));
$this->assertEquals($outline2->id(), $outline->id());
}
/**
* Test uninstall and reinstall of the outline module.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function testUninstallReinstall() {
$outline = $this->createOutline();
// Field storages and fields attached to outline entry bundles should be
// removed when the module is uninstalled.
$field_name = mb_strtolower($this->randomMachineName() . '_field_name');
$storage_definition = [
'field_name' => $field_name,
'entity_type' => 'outline_entry',
'type' => 'text',
'cardinality' => 4,
];
FieldStorageConfig::create($storage_definition)->save();
$field_definition = [
'field_name' => $field_name,
'entity_type' => 'outline_entry',
'bundle' => $outline->id(),
'label' => $this->randomMachineName() . '_label',
];
FieldConfig::create($field_definition)->save();
// Remove the third party setting from the memory copy of the outline.
// We keep this invalid copy around while the outline module is not even
// installed for testing below.
$outline->unsetThirdPartySetting('outline_crud', 'foo');
$this->container->get('module_installer')->uninstall(['outline']);
$this->container->get('module_installer')->install(['outline']);
// Now create a outline with the same name. All fields connected to this
// outline name should have been removed when the module was uninstalled.
// Creating a new field with the same name and an instance of this field on
// the same bundle name should be successful.
$outline->enforceIsNew()->save();
FieldStorageConfig::create($storage_definition)->save();
FieldConfig::create($field_definition)->save();
}
}
