Commit 0335236d authored by Evgenii Nikitin's avatar Evgenii Nikitin Committed by Hemant Gupta
Browse files

Issue #3239605 by sinn, guptahemant, sardara: Add tests for purge_control module

parent 2d69ec8c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@
        "drupal/core": "^8.8.6 || ^9",
        "drupal/purge": "^3.0.0"
    },
    "require-dev": {
        "drush/drush": "^10"
    },
    "extra": {
        "drush": {
            "services": {
+65 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\purge_control\Functional;

use Drupal\Core\Test\RefreshVariablesTrait;
use Drupal\Tests\BrowserTestBase;
use Drush\TestTraits\DrushTestTrait;

/**
 * Tests drush commands.
 *
 * @coversDefaultClass \Drupal\purge_control\Commands\PurgeControlCommands
 *
 * @group purge_control
 */
class PurgeControlCommandsTest extends BrowserTestBase {

  use DrushTestTrait;
  use RefreshVariablesTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['purge_control'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Test "purge-control" drush command.
   *
   * @covers ::purgeControl
   */
  public function testPurgeControl() {
    // Assert purge disabling.
    $this->drush('purge-control', ['disable-purge']);
    $this->assertStringContainsString('Purging is disabled.', $this->getErrorOutput());
    $this->assertTrue(\Drupal::config('purge_control.settings')->get('disable_purge'));

    // Assert purge enabling.
    $this->drush('purge-control', ['enable-purge']);
    $this->assertStringContainsString('Purging is enabled.', $this->getErrorOutput());
    $this->refreshVariables();
    $this->assertFalse(\Drupal::config('purge_control.settings')->get('disable_purge'));

    // Assert disabling of automation.
    $this->drush('purge-control', ['disable-automation']);
    $this->assertStringContainsString('Automated purge control is disabled.', $this->getErrorOutput());
    $this->refreshVariables();
    $this->assertFalse(\Drupal::config('purge_control.settings')->get('purge_auto_control'));

    // Assert enabling of automation.
    $this->drush('purge-control', ['enable-automation']);
    $this->assertStringContainsString('Automated purge control is enabled.', $this->getErrorOutput());
    $this->refreshVariables();
    $this->assertTrue(\Drupal::config('purge_control.settings')->get('purge_auto_control'));

    // Assert wrong option.
    $this->drush('purge-control', ['wrong-parameter']);
    $this->assertStringContainsString('Invalid operation.', $this->getErrorOutput());
  }

}
+59 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\purge_control\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;

/**
 * Tests cron execution for purge_control.
 *
 * @group purge_control
 */
class PurgeControlCronTest extends BrowserTestBase {

  use CronRunTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['purge_control'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Purge control service instance.
   *
   * @var \Drupal\purge_control\Services\PurgeControl
   */
  protected $purgeControl;

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

    $this->purgeControl = \Drupal::service('purge_control.purge_control');
  }

  /**
   * Test purge_control_cron function.
   */
  public function testCron() {
    // Disabled purge won't be enabled if automation is disabled.
    $this->purgeControl->disablePurge();
    $this->purgeControl->setAutomation(FALSE);
    $this->cronRun();
    $this->assertTrue($this->config('purge_control.settings')->get('disable_purge'));

    // Disabled purge will be enabled if automation is enabled.
    $this->purgeControl->setAutomation(TRUE);
    $this->cronRun();
    $this->assertFalse($this->config('purge_control.settings')->get('disable_purge'));
  }

}
+72 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\purge_control\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests configuration form.
 *
 * @coversDefaultClass \Drupal\purge_control\PurgeControlSettings
 *
 * @group purge_control
 */
class PurgeControlSettingsTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'purge_control',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Test the settings form.
   */
  public function testSettingsForm() {
    // Anonymous user doesn't have access to the configuration form.
    $this->drupalGet('admin/config/development/performance/purge/purge-control');
    $this->assertSession()->statusCodeEquals(403);

    // Authenticated user doesn't have access to the configuration form.
    $authenticated_user = $this->drupalCreateUser();
    $this->drupalLogin($authenticated_user);
    $this->drupalGet('admin/config/development/performance/purge/purge-control');
    $this->assertSession()->statusCodeEquals(403);

    $admin_user = $this->drupalCreateUser(['administer site configuration']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/config/development/performance/purge/purge-control');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Purge Control Settings');

    // Assert default settings.
    $this->assertSession()->checkboxChecked('purge_auto_control');
    $this->assertSession()->checkboxNotChecked('disable_purge');

    $config = \Drupal::config('purge_control.settings');
    $this->assertTrue($config->get('purge_auto_control'));
    $this->assertFalse($config->get('disable_purge'));

    // Assert update of configuration.
    $edit = [
      'purge_auto_control' => FALSE,
      'disable_purge'  => TRUE,
    ];
    $this->submitForm($edit, 'Save configuration');

    $this->assertSession()->pageTextContains('The configuration options have been saved.');
    $this->assertSession()->checkboxNotChecked('purge_auto_control');
    $this->assertSession()->checkboxChecked('disable_purge');

    $config = \Drupal::config('purge_control.settings');
    $this->assertFalse($config->get('purge_auto_control'));
    $this->assertTrue($config->get('disable_purge'));
  }

}
+88 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\purge_control\Kernel\DiagnosticCheck;

use Drupal\purge\Plugin\Purge\DiagnosticCheck\PluginManager;
use Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticCheckInterface;
use Drupal\Tests\purge\Kernel\KernelPluginManagerTestBase;

/**
 * Tests "Purge enabled" diagnostic check plugin.
 *
 * @coversDefaultClass \Drupal\purge_control\Plugin\Purge\DiagnosticCheck\PurgeEnabledCheck
 *
 * @group purge_control
 */
class PurgeEnabledCheckTest extends KernelPluginManagerTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['purge_control'];

  /**
   * {@inheritdoc}
   */
  protected $pluginManagerClass = PluginManager::class;

  /**
   * {@inheritdoc}
   */
  public function setUp($switch_to_memory_queue = TRUE): void {
    parent::setUp($switch_to_memory_queue);

    $this->installConfig(['purge_control']);
  }

  /**
   * Test the plugin definition.
   */
  public function testDefinition(): void {
    // All metadata from \Drupal\purge\Annotation\PurgeDiagnosticCheck.
    $annotationFields = [
      'class',
      'dependent_purger_plugins',
      'dependent_queue_plugins',
      'description',
      'id',
      'provider',
      'title',
    ];

    // Plugin definition exists.
    $definitions = $this->pluginManager->getDefinitions();
    $this->assertArrayHasKey('purge_enabled', $definitions);

    // Annotation fields exist and only those that are needed.
    $definition_fields = array_keys($definitions['purge_enabled']);
    sort($definition_fields);
    $this->assertEquals($annotationFields, $definition_fields);

    // Assert definition values.
    $this->assertEquals('Purge enabled.', $definitions['purge_enabled']['title']);
    $this->assertEquals('Checks to see if the puring is enabled.', $definitions['purge_enabled']['description']);
    $this->assertEmpty($definitions['purge_enabled']['dependent_queue_plugins']);
    $this->assertEmpty($definitions['purge_enabled']['dependent_purger_plugins']);
  }

  /**
   * Test the plugin logic.
   *
   * @covers ::run
   */
  public function testRun() {
    // By default purging is enabled.
    $plugin = $this->pluginManager->createInstance('purge_enabled');
    $this->assertEquals(DiagnosticCheckInterface::SEVERITY_OK, $plugin->run());
    $this->assertEquals('Purging is enabled.', $plugin->getRecommendation());

    // Purging is disabled.
    $this->config('purge_control.settings')
      ->set('disable_purge', TRUE)
      ->save();
    $plugin = $this->pluginManager->createInstance('purge_enabled');
    $this->assertEquals(DiagnosticCheckInterface::SEVERITY_ERROR, $plugin->run());
    $this->assertEquals('Purging is disabled.', $plugin->getRecommendation());
  }

}
Loading