clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,306 @@
<?php
namespace Drupal\Tests\log\Functional;
use Drupal\log\Entity\LogInterface;
/**
* Tests the Log form actions.
*
* @group Log
*/
class LogActionsTest extends LogTestBase {
/**
* Tests cloning a single log.
*/
public function testCloneSingleLog() {
// Create new user.
$original_user = $this->createUser([], 'Original user');
$this->assertNotEquals($this->loggedInUser->id(), $original_user->id());
// Create log.
$timestamp = \Drupal::time()->getRequestTime();
$log = $this->createLogEntity([
'uid' => $original_user->id(),
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(1, $num_of_logs, 'There is one log in the system.');
$edit = [];
$edit['action'] = 'log_clone_action';
$edit['log_bulk_form[0]'] = TRUE;
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to clone this log?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$new_timestamp = strtotime(date('Y-n-j', strtotime('+1 day', $timestamp)));
$edit_clone = [];
$edit_clone['date[date]'] = date('Y-m-d', $new_timestamp);
$this->submitForm($edit_clone, $this->t('Clone'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Cloned 1 log'));
/** @var \Drupal\log\Entity\LogInterface[] $logs */
$logs = $this->storage->loadMultiple();
$this->assertEquals(2, count($logs), 'There are two logs in the system.');
$this->assertEquals($this->loggedInUser->id(), $logs[2]->getOwnerId(), 'Owner on the new log has been updated.');
$this->assertEquals($new_timestamp, $logs[2]->get('timestamp')->value, 'Timestamp on the new log has been updated.');
}
/**
* Tests cloning multiple logs.
*/
public function testCloneMultipleLogs() {
// Create new user.
$original_user = $this->createUser([], 'Original user');
$this->assertNotEquals($this->loggedInUser->id(), $original_user->id());
// Create logs.
$timestamp = \Drupal::time()->getRequestTime();
for ($i = 0; $i < 3; $i++) {
$log = $this->createLogEntity([
'uid' => $original_user->id(),
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
}
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(3, $num_of_logs, 'There are three logs in the system.');
$edit = [];
$edit['action'] = 'log_clone_action';
for ($i = 0; $i < 3; $i++) {
$edit['log_bulk_form[' . $i . ']'] = TRUE;
}
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to clone these logs?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$new_timestamp = strtotime(date('Y-n-j', strtotime('+1 day', $timestamp)));
$edit_clone = [];
$edit_clone['date[date]'] = date('Y-m-d', $new_timestamp);
$this->submitForm($edit_clone, $this->t('Clone'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Cloned 3 logs'));
$logs = $this->storage->loadMultiple();
$this->assertEquals(6, count($logs), 'There are six logs in the system.');
for ($i = 1; $i <= 3; $i++) {
$this->assertEquals($this->loggedInUser->id(), $logs[3 + $i]->getOwnerId(), 'Owner on the new log has been updated');
$this->assertEquals($new_timestamp, $logs[3 + $i]->get('timestamp')->value, 'Timestamp on the new log has been updated.');
}
}
/**
* Tests rescheduling a single log to an absolute date.
*/
public function testRescheduleSingleLogAbsolute() {
$timestamp = \Drupal::time()->getRequestTime();
$log = $this->createLogEntity([
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(1, $num_of_logs, 'There is one log in the system.');
$edit = [];
$edit['action'] = 'log_reschedule_action';
$edit['log_bulk_form[0]'] = TRUE;
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to reschedule this log?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$new_timestamp = strtotime(date('Y-n-j', strtotime('+1 day', $timestamp)));
$edit_reschedule = [];
$edit_reschedule['date[date]'] = date('Y-m-d', $new_timestamp);
$this->submitForm($edit_reschedule, $this->t('Reschedule'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Rescheduled 1 log'));
$logs = $this->storage->loadMultiple();
$this->assertEquals(1, $num_of_logs, 'There is one log in the system.');
$log = reset($logs);
$this->assertEquals($new_timestamp, $log->get('timestamp')->value, 'Timestamp on the log has changed.');
$this->assertEquals('pending', $log->get('status')->value, 'Log has been set to pending.');
}
/**
* Tests rescheduling multiple logs to an absolute date.
*/
public function testRescheduleMultipleLogsAbsolute() {
$timestamp = \Drupal::time()->getRequestTime();
for ($i = 0; $i < 3; $i++) {
$timestamp = strtotime(date('Y-n-j', strtotime('+1 day', $timestamp)));
$log = $this->createLogEntity([
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
}
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(3, $num_of_logs, 'There are three logs in the system.');
$edit = [];
$edit['action'] = 'log_reschedule_action';
for ($i = 0; $i < 3; $i++) {
$edit['log_bulk_form[' . $i . ']'] = TRUE;
}
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to reschedule these logs?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$new_timestamp = strtotime('+1 day', $timestamp);
$edit_reschedule = [];
$edit_reschedule['date[date]'] = date('Y-m-d', $new_timestamp);
$this->submitForm($edit_reschedule, $this->t('Reschedule'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Rescheduled 3 logs'));
$logs = $this->storage->loadMultiple();
$this->assertEquals(3, count($logs), 'There are three logs in the system.');
foreach ($logs as $log) {
$this->assertEquals($new_timestamp, $log->get('timestamp')->value, 'Timestamp on the log has changed.');
$this->assertEquals('pending', $log->get('status')->value, 'Log has been set to pending.');
}
}
/**
* Tests rescheduling a single log to an relative date.
*/
public function testRescheduleSingleLogRelative() {
$timestamp = \Drupal::time()->getRequestTime();
$log = $this->createLogEntity([
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(1, $num_of_logs, 'There is one log in the system.');
$edit = [];
$edit['action'] = 'log_reschedule_action';
$edit['log_bulk_form[0]'] = TRUE;
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to reschedule this log?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$edit_reschedule = [];
$edit_reschedule['type_of_date'] = 1;
$this->submitForm($edit_reschedule, $this->t('Reschedule'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log/reschedule');
$this->assertSession()->pageTextContains($this->t('Please enter the amount of time for rescheduling.'));
$new_timestamp = strtotime('+1 day', $timestamp);
$edit_reschedule = [];
$edit_reschedule['type_of_date'] = 1;
$edit_reschedule['amount'] = 1;
$edit_reschedule['time'] = 'day';
$this->submitForm($edit_reschedule, $this->t('Reschedule'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Rescheduled 1 log'));
$logs = $this->storage->loadMultiple();
$this->assertEquals(1, $num_of_logs, 'There is one log in the system.');
$log = reset($logs);
$this->assertEquals($new_timestamp, $log->get('timestamp')->value, 'Timestamp on the log has changed.');
$this->assertEquals('pending', $log->get('status')->value, 'Log has been set to pending.');
}
/**
* Tests rescheduling multiple logs to an relative date.
*/
public function testRescheduleMultipleLogsRelative() {
$timestamp = \Drupal::time()->getRequestTime();
$expected_timestamps = [];
for ($i = 0; $i < 3; $i++) {
$timestamp = strtotime('+1 day', $timestamp);
$log = $this->createLogEntity([
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
'timestamp' => $timestamp,
]);
$log->save();
// Save the expected timestamp for the log.
$new_timestamp = strtotime('-1 month', $timestamp);
$expected_timestamps[$log->id()] = $new_timestamp;
}
$num_of_logs = $this->storage->getQuery()->count()->accessCheck(TRUE)->execute();
$this->assertEquals(3, $num_of_logs, 'There are three logs in the system.');
$edit = [];
$edit['action'] = 'log_reschedule_action';
for ($i = 0; $i < 3; $i++) {
$edit['log_bulk_form[' . $i . ']'] = TRUE;
}
$this->drupalGet('admin/content/log');
$this->submitForm($edit, $this->t('Apply to selected items'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Are you sure you want to reschedule these logs?'));
$this->assertSession()->pageTextContains($this->t('New date'));
$edit_reschedule = [];
$edit_reschedule['type_of_date'] = 1;
$edit_reschedule['amount'] = -1;
$edit_reschedule['time'] = 'month';
$this->submitForm($edit_reschedule, $this->t('Reschedule'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('admin/content/log');
$this->assertSession()->pageTextContains($this->t('Rescheduled 3 logs'));
$logs = $this->storage->loadMultiple();
$this->assertEquals(3, count($logs), 'There are three logs in the system.');
$log_timestamps = array_map(function (LogInterface $log) {
return $log->get('timestamp')->value;
}, $logs);
$this->assertEquals($expected_timestamps, $log_timestamps, 'Logs have been rescheduled');
}
}
@@ -0,0 +1,113 @@
<?php
namespace Drupal\Tests\log\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Tests the Log CRUD.
*
* @group Log
*/
class LogCRUDTest extends LogTestBase {
use StringTranslationTrait;
/**
* Fields are displayed correctly.
*/
public function testFieldsVisibility() {
$this->drupalGet('log/add/default');
$this->assertSession()->statusCodeEquals('200');
$assert_session = $this->assertSession();
$assert_session->fieldExists('name[0][value]');
$assert_session->fieldExists('timestamp[0][value][date]');
$assert_session->fieldExists('timestamp[0][value][time]');
$assert_session->fieldExists('status');
$assert_session->fieldExists('revision_log_message[0][value]');
$assert_session->fieldExists('uid[0][target_id]');
$assert_session->fieldExists('created[0][value][date]');
$assert_session->fieldExists('created[0][value][time]');
}
/**
* Create Log entity.
*/
public function testCreateLog() {
$assert_session = $this->assertSession();
$name = $this->randomMachineName();
$edit = [
'name[0][value]' => $name,
];
$this->drupalGet('log/add/default');
$this->submitForm($edit, $this->t('Save'));
$result = $this->storage
->getQuery()
->range(0, 1)
->accessCheck(TRUE)
->execute();
$log_id = reset($result);
$log = $this->storage->load($log_id);
$this->assertEquals($log->get('name')->value, $name, 'Log has been saved.');
$assert_session->pageTextContains("Saved log: $name");
$assert_session->pageTextContains($name);
}
/**
* Display log entity.
*/
public function testViewLog() {
$edit = [
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
];
$log = $this->createLogEntity($edit);
$log->save();
$this->drupalGet($log->toUrl('canonical'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($edit['name']);
$this->assertSession()->responseContains(\Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime()));
}
/**
* Edit log entity.
*/
public function testEditLog() {
$log = $this->createLogEntity();
$log->save();
$edit = [
'name[0][value]' => $this->randomMachineName(),
];
$this->drupalGet($log->toUrl('edit-form'));
$this->submitForm($edit, $this->t('Save'));
$this->assertSession()->pageTextContains($edit['name[0][value]']);
}
/**
* Delete log entity.
*/
public function testDeleteLog() {
$log = $this->createLogEntity();
$log->save();
$label = $log->getName();
$log_id = $log->id();
$this->drupalGet($log->toUrl('delete-form'));
$this->submitForm([], $this->t('Delete'));
$this->assertSession()->responseContains($this->t('The @entity-type %label has been deleted.', [
'@entity-type' => $log->getEntityType()->getSingularLabel(),
'%label' => $label,
]));
$this->assertNull($this->storage->load($log_id));
}
}
@@ -0,0 +1,100 @@
<?php
namespace Drupal\Tests\log\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Tests the Log name pattern.
*
* @group Log
*/
class LogNamePatternTest extends LogTestBase {
use StringTranslationTrait;
/**
* Tests creating a log entity without name.
*/
public function testCreateLogWithoutName() {
$edit = [
'status' => 'done',
];
$this->drupalGet('log/add/name_pattern');
$this->submitForm($edit, $this->t('Save'));
$result = $this->storage
->getQuery()
->range(0, 1)
->accessCheck(TRUE)
->execute();
$log_id = reset($result);
$log = $this->storage->load($log_id);
$this->assertEquals($log->label(), $log_id . ' done', 'Log name is the pattern and not the name.');
$this->drupalGet($log->toUrl('canonical'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($log_id);
}
/**
* Tests creating a log entity with name.
*/
public function testCreateLogWithName() {
$name = $this->randomMachineName();
$edit = [
'name[0][value]' => $name,
];
$this->drupalGet('log/add/name_pattern');
$this->submitForm($edit, $this->t('Save'));
$result = $this->storage
->getQuery()
->range(0, 1)
->accessCheck(TRUE)
->execute();
$log_id = reset($result);
$log = $this->storage->load($log_id);
$this->assertEquals($log->get('name')->value, $name, 'Log name is the pattern and not the name.');
$this->drupalGet($log->toUrl('canonical'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($name);
}
/**
* Edit log entity.
*/
public function testEditLog() {
$log = $this->createLogEntity(['type' => 'name_pattern']);
$log->save();
// Test that a manually set name does not get overwritten.
$edit = [
'name[0][value]' => $this->randomMachineName(),
];
$this->drupalGet($log->toUrl('edit-form'));
$this->submitForm($edit, $this->t('Save'));
$this->assertSession()->pageTextContains($edit['name[0][value]']);
// Test that clearing the name forces it to be auto-generated.
$edit = [
'name[0][value]' => '',
'status' => 'pending',
];
$this->drupalGet($log->toUrl('edit-form'));
$this->submitForm($edit, $this->t('Save'));
$this->assertSession()->pageTextContains($log->id() . ' pending');
// Test that updating a log with an auto-generated name automatically
// updates the name.
$edit = [
'status' => 'done',
];
$this->drupalGet($log->toUrl('edit-form'));
$this->submitForm($edit, $this->t('Save'));
$this->assertSession()->pageTextContains($log->id() . ' done');
}
}
@@ -0,0 +1,99 @@
<?php
namespace Drupal\Tests\log\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
/**
* Tests the Log CRUD.
*/
abstract class LogTestBase extends BrowserTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The log storage handler.
*
* @var \Drupal\group\Entity\Storage\GroupRoleStorageInterface
*/
protected $storage;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'entity',
'user',
'log',
'log_test',
'field',
'text',
];
/**
* A test user with administrative privileges.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = $this->container->get('entity_type.manager');
$this->storage = $entity_type_manager->getStorage('log');
$this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
$this->drupalLogin($this->adminUser);
drupal_flush_all_caches();
}
/**
* Gets the permissions for the admin user.
*
* @return string[]
* The permissions.
*/
protected function getAdministratorPermissions() {
return [
'access administration pages',
'administer log',
'view any log',
'create default log',
'view any default log',
'update own default log',
'update any default log',
'delete own default log',
'delete any default log',
];
}
/**
* Creates a log entity.
*
* @param array $values
* Array of values to feed the entity.
*
* @return \Drupal\log\Entity\LogInterface
* The log entity.
*/
protected function createLogEntity(array $values = []) {
$entity = $this->storage->create($values + [
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'type' => 'default',
]);
return $entity;
}
}