You are here

InactiveDeleteNotificationTest.php in Auto Purge Users 8.3

File

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

declare (strict_types=1);
namespace Drupal\Tests\purge_users\Functional;

use Drupal\Core\Test\AssertMailTrait;

/**
 * Purge users whose account has not been activated for a specific period.
 *
 * - Purge method: Delete the account and its content.
 * - Disregard inactive/blocked users unselected.
 * - User Deletion Notification selected.
 *
 * @group purge_users
 */
class InactiveDeleteNotificationTest 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', '1')
      ->set('user_inactive_period', 'year')
      ->set('enabled_inactive_users', TRUE)
      ->set('purge_user_cancel_method', 'user_cancel_delete')
      ->set('send_email_notification', TRUE)
      ->set('send_email_user_before_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(2, $captured_emails);
    $this
      ->assertEquals(static::getDeletionExpectedEmail(), $captured_emails[0]);
    $this
      ->assertEquals(static::getCancellationExpectedEmail(), $captured_emails[1]);

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

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

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

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

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

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

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

    // The user is created 2 years ago, never logged in, status = 0.
    // Expected to be deleted.
    $this->blockedUserToDelete = $this
      ->createUser([], NULL, FALSE, [
      'mail' => 'testemail@test.test',
      'created' => strtotime('-2 year'),
      'login' => 0,
    ]);
    $this->blockedUserToDelete->status = 0;
    $this->blockedUserToDelete
      ->save();
  }

}

Classes

Namesort descending Description
InactiveDeleteNotificationTest Purge users whose account has not been activated for a specific period.