You are here

NotificationBeforeUserDeletionTest.php in Auto Purge Users 8.3

File

tests/src/Functional/NotificationBeforeUserDeletionTest.php
View source
<?php

namespace Drupal\Tests\purge_users\Functional;

use Drupal\Core\Test\AssertMailTrait;

/**
 * Notification is sent before users are purged.
 *
 * - Purge method: Delete the account and its content.
 * - Disregard inactive/blocked users unselected.
 * - User Notification before deletion selected.
 *
 * @group purge_users
 */
class NotificationBeforeUserDeletionTest extends SettingsBase {
  use AssertMailTrait;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();

    // Set the users for this scenario.
    $this
      ->addAdminUser();
    $this
      ->createTestUser();

    // Set the basic configuration and add the specific changes.
    $this
      ->setBasicConfig();
    $this
      ->config('purge_users.settings')
      ->set('user_inactive_value', '5')
      ->set('user_inactive_period', 'days')
      ->set('enabled_inactive_users', TRUE)
      ->set('user_before_notification_value', '2')
      ->set('user_before_notification_period', 'days')
      ->set('send_email_user_before_notification', TRUE)
      ->set('send_email_notification', FALSE)
      ->save();

    // Set storage.
    $this->userStorage = $this->container
      ->get('entity_type.manager')
      ->getStorage('user');
  }

  /**
   * {@inheritdoc}
   */
  protected function checkConfirmFormResults() : void {

    // Test email notification.
    $captured_emails = $this
      ->getMails();
    $this
      ->assertCount(0, $captured_emails);

    // Verify users.
    $account = $this->userStorage
      ->load($this->admin
      ->id());
    $this
      ->assertNotNull($account);

    // Blocked user is not deleted.
    $account = $this->userStorage
      ->load($this->blockedUserToDelete
      ->id());
    $this
      ->assertNotNull($account);
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCronResults() : void {

    // Test email notification.
    $captured_emails = $this
      ->getMails();
    $this
      ->assertCount(1, $captured_emails);
    $this
      ->assertEquals(static::getCancellationExpectedEmail(), $captured_emails[0]);

    // Verify users.
    $account = $this->userStorage
      ->load($this->admin
      ->id());
    $this
      ->assertNotNull($account);

    // Blocked user is not deleted.
    $account = $this->userStorage
      ->load($this->blockedUserToDelete
      ->id());
    $this
      ->assertNotNull($account);
  }

  /**
   * Set user to be deleted and add email for notification.
   */
  protected function createTestUser() : void {

    // The user is created 3 days ago, never logged in
    // and status = 0.
    $this->blockedUserToDelete = $this
      ->createUser([], NULL, FALSE, [
      'mail' => 'testemail@test.test',
      'created' => strtotime('-3 day'),
      'login' => 0,
    ]);
    $this->blockedUserToDelete->status = 0;
    $this->blockedUserToDelete
      ->save();
  }

}

Classes

Namesort descending Description
NotificationBeforeUserDeletionTest Notification is sent before users are purged.