Commit a2ffbc64 authored by George's avatar George
Browse files

Issue #3096738 by jhedstrom: Expiry condition should implement getCacheMaxAge

parent f0c6b3ae
Loading
Loading
Loading
Loading
+50 −5
Original line number Diff line number Diff line
@@ -6,9 +6,13 @@

namespace Drupal\block_scheduler\Plugin\Condition;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'Expiry' condition.
@@ -18,7 +22,32 @@ use Drupal\Core\Datetime\DrupalDateTime;
 *   label = @Translation("Expiry")
 * )
 */
class Expiry extends ConditionPluginBase {
class Expiry extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->setTime($container->get('datetime.time'));
    return $instance;
  }

  /**
   * Setter for the time service.
   *
   * @param \Drupal\Component\Datetime\TimeInterface $time
   */
  protected function setTime(TimeInterface $time) {
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
@@ -71,16 +100,14 @@ class Expiry extends ConditionPluginBase {
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (is_object($form_state->getValue('start'))) {
      $this->configuration['start'] = $form_state->getValue('start')
        ->getTimestamp();
      $this->configuration['start'] = $form_state->getValue('start')->getTimestamp();
    }
    else {
      $this->configuration['start'] = '';
    }

    if (is_object($form_state->getValue('end'))) {
      $this->configuration['end'] = $form_state->getValue('end')
        ->getTimestamp();
      $this->configuration['end'] = $form_state->getValue('end')->getTimestamp();
    }
    else {
      $this->configuration['end'] = '';
@@ -116,4 +143,22 @@ class Expiry extends ConditionPluginBase {
    return ['start' => '', 'end' => ''] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    $current_time = $this->time->getRequestTime();
    $max_age = Cache::PERMANENT;

    // If the published on date is in the future, use that.
    if ($this->configuration['start'] > $current_time) {
      $max_age = $this->configuration['start'] - $current_time;
    }
    elseif ((int) $this->configuration['end'] > $current_time) {
      // If the unpublished time is in the future, use that.
      $max_age = $this->configuration['end'] - $current_time;
    }

    return Cache::mergeMaxAges(parent::getCacheMaxAge(), $max_age);
  }
}
+97 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\block_scheduler\Unit;

use Drupal\block_scheduler\Plugin\Condition\Expiry;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;

/**
 * Class ExpiryTest
 *
 * @group auto_block_scheduler
 *
 * @coversDefaultClass \Drupal\block_scheduler\Plugin\Condition\Expiry
 */
class ExpiryTest extends UnitTestCase {

  /**
   * @covers ::getCacheMaxAge
   *
   * @dataProvider providerGetCacheMaxAge
   */
  public function testGetCacheMaxAge($current_time, $published_on, $unpublished_on, $expected_max_age) {
    $configuration = [
      'start' => $published_on,
      'end' => $unpublished_on,
    ];

    $container = new Container();

    // Mock the time service.
    $time = $this->prophesize(TimeInterface::class);
    $time->getRequestTime()->willReturn($current_time);
    $container->set('datetime.time', $time->reveal());

    $fixture = Expiry::create($container, $configuration, 'foo', []);
    $this->assertEquals($expected_max_age, $fixture->getCacheMaxAge());
  }

  public function providerGetCacheMaxAge() {
    $cases = [];

    $cases['empty_dates'] = [
      10000,
      0,
      0,
      Cache::PERMANENT,
    ];

    $cases['old published on, no end date'] = [
      10000,
      5000,
      0,
      Cache::PERMANENT,
    ];

    $cases['future published on, no end date'] = [
      10000,
      11000,
      0,
      1000,
    ];

    $cases['no publish, has unpublish'] = [
      10000,
      0,
      12000,
      2000,
    ];

    $cases['past publish, has upublish'] = [
      10000,
      5000,
      13000,
      3000,
    ];

    $cases['future both'] = [
      10000,
      11000,
      12000,
      1000,
    ];

    $cases['past both'] = [
      10000,
      5000,
      6000,
      Cache::PERMANENT,
    ];

    return $cases;
  }

}