You are here

SchedulerNonEnabledTypeTest.php in Scheduler 2.x

Same filename and directory in other branches
  1. 8 tests/src/Functional/SchedulerNonEnabledTypeTest.php

File

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

namespace Drupal\Tests\scheduler\Functional;


/**
 * Tests entity types which are not enabled for scheduling.
 *
 * @group scheduler
 */
class SchedulerNonEnabledTypeTest extends SchedulerBrowserTestBase {

  /**
   * Tests the publish_enable and unpublish_enable entity type settings.
   *
   * @dataProvider dataNonEnabledType()
   */
  public function testNonEnabledType($id, $entityTypeId, $description, $publishing_enabled, $unpublishing_enabled) {
    $this
      ->drupalLogin($this->adminUser);
    $entityType = $this
      ->entityTypeObject($entityTypeId, 'non-enabled');
    $bundle = $entityType
      ->id();
    $storage = $this
      ->entityStorageObject($entityTypeId);
    $titleField = $entityTypeId == 'media' ? 'name' : 'title';

    // The 'default' case specifically checks the behavior of the unchanged
    // settings, so only change these when not running the default test.
    if ($description != 'Default') {
      $entityType
        ->setThirdPartySetting('scheduler', 'publish_enable', $publishing_enabled)
        ->setThirdPartySetting('scheduler', 'unpublish_enable', $unpublishing_enabled)
        ->save();
    }

    // When publishing and/or unpublishing are not enabled but the 'required'
    // setting remains on, the entity must be able to be saved without a date.
    $entityType
      ->setThirdPartySetting('scheduler', 'publish_required', !$publishing_enabled)
      ->save();
    $entityType
      ->setThirdPartySetting('scheduler', 'unpublish_required', !$unpublishing_enabled)
      ->save();

    // Allow dates in the past to be valid on saving the entity, to simplify the
    // testing process.
    $entityType
      ->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
      ->save();

    // Create a new entity via the add/bundle url, and check that the correct
    // fields are displayed on the form depending on the enabled settings.
    $this
      ->drupalGet($this
      ->entityAddUrl($entityTypeId, $bundle));
    if ($publishing_enabled) {
      $this
        ->assertSession()
        ->fieldExists('publish_on[0][value][date]');
    }
    else {
      $this
        ->assertSession()
        ->fieldNotExists('publish_on[0][value][date]');
    }
    if ($unpublishing_enabled) {
      $this
        ->assertSession()
        ->fieldExists('unpublish_on[0][value][date]');
    }
    else {
      $this
        ->assertSession()
        ->fieldNotExists('unpublish_on[0][value][date]');
    }

    // Fill in the title field and check that the entity can be saved OK.
    $title = $id . 'a - ' . $description;
    $this
      ->submitForm([
      "{$titleField}[0][value]" => $title,
    ], 'Save');
    $this
      ->assertSession()
      ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');

    // Create an unpublished entity with a publishing date, which mimics what
    // could be done by a third-party module, or a by-product of the entity type
    // being enabled for publishing then being disabled before it got published.
    $title = $id . 'b - ' . $description;
    $values = [
      "{$titleField}" => $title,
      'status' => FALSE,
      'publish_on' => $this->requestTime - 120,
    ];
    $entity = $this
      ->createEntity($entityTypeId, $bundle, $values);

    // Check that the entity can be edited and saved OK.
    $this
      ->drupalGet($entity
      ->toUrl('edit-form'));
    $this
      ->submitForm([], 'Save');
    $this
      ->assertSession()
      ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');

    // Run cron and display the dblog.
    $this
      ->cronRun();
    $this
      ->drupalGet('admin/reports/dblog');

    // Reload the entity.
    $storage
      ->resetCache([
      $entity
        ->id(),
    ]);
    $entity = $storage
      ->load($entity
      ->id());

    // Check if the entity has been published or remains unpublished.
    if ($publishing_enabled) {
      $this
        ->assertTrue($entity
        ->isPublished(), "The unpublished entity '{$title}' should now be published");
    }
    else {
      $this
        ->assertFalse($entity
        ->isPublished(), "The unpublished entity '{$title}' should remain unpublished");
    }

    // Do the same for unpublishing - create a published entity with an
    // unpublishing date in the future, to be valid for editing and saving.
    $title = $id . 'c - ' . $description;
    $values = [
      "{$titleField}" => $title,
      'status' => TRUE,
      'unpublish_on' => $this->requestTime + 180,
    ];
    $entity = $this
      ->createEntity($entityTypeId, $bundle, $values);

    // Check that the entity can be edited and saved.
    $this
      ->drupalGet($entity
      ->toUrl('edit-form'));
    $this
      ->submitForm([], 'Save');
    $this
      ->assertSession()
      ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');

    // Create a published entity with a date in the past, then run cron.
    $title = $id . 'd - ' . $description;
    $values = [
      "{$titleField}" => $title,
      'status' => TRUE,
      'unpublish_on' => $this->requestTime - 120,
    ];
    $entity = $this
      ->createEntity($entityTypeId, $bundle, $values);
    $this
      ->cronRun();
    $this
      ->drupalGet('admin/reports/dblog');

    // Reload the entity.
    $storage
      ->resetCache([
      $entity
        ->id(),
    ]);
    $entity = $storage
      ->load($entity
      ->id());

    // Check if the entity has been unpublished or remains published.
    if ($unpublishing_enabled) {
      $this
        ->assertFalse($entity
        ->isPublished(), "The published entity '{$title}' should now be unpublished");
    }
    else {
      $this
        ->assertTrue($entity
        ->isPublished(), "The published entity '{$title}' should remain published");
    }

    // Display the full content list and the scheduled list. Calls to these
    // pages are for information and debug only.
    switch ($entityTypeId) {
      case 'node':
        $this
          ->drupalGet('admin/content');
        $this
          ->drupalGet('admin/content/scheduled');
        break;
      case 'media':
        $this
          ->drupalGet('admin/content/media');
        $this
          ->drupalGet('admin/content/media/scheduled');
        break;
      case 'commerce_product':
        $this
          ->drupalGet('admin/commerce/products');
        $this
          ->drupalGet('admin/commerce/products/scheduled');
        break;
    }
  }

  /**
   * Provides data for testNonEnabledType().
   *
   * @return array
   *   Each item in the test data array has the follow elements:
   *     id                     - (int) a sequential id for use in titles
   *     entityTypeId           - (string) 'node', 'media' or 'commerce_product'
   *     description            - (string) describing the scenario being checked
   *     publishing_enabled     - (bool) whether publishing is enabled
   *     unpublishing_enabled   - (bool) whether unpublishing is enabled
   */
  public function dataNonEnabledType() {
    $data = [];
    foreach ($this
      ->dataStandardEntityTypes() as $key => $values) {
      $entityTypeId = $values[0];

      // By default check that the scheduler date fields are not displayed.
      $data["{$key}-1"] = [
        1,
        $entityTypeId,
        'Default',
        FALSE,
        FALSE,
      ];

      // Explicitly disable this content type for both settings.
      $data["{$key}-2"] = [
        2,
        $entityTypeId,
        'Disabling both settings',
        FALSE,
        FALSE,
      ];

      // Turn on scheduled publishing only.
      $data["{$key}-3"] = [
        3,
        $entityTypeId,
        'Enabling publishing only',
        TRUE,
        FALSE,
      ];

      // Turn on scheduled unpublishing only.
      $data["{$key}-4"] = [
        4,
        $entityTypeId,
        'Enabling unpublishing only',
        FALSE,
        TRUE,
      ];

      // For completeness turn on both scheduled publishing and unpublishing.
      $data["{$key}-5"] = [
        5,
        $entityTypeId,
        'Enabling both publishing and unpublishing',
        TRUE,
        TRUE,
      ];
    }
    return $data;
  }

}

Classes

Namesort descending Description
SchedulerNonEnabledTypeTest Tests entity types which are not enabled for scheduling.