diff --git a/src/Tests/Invalidation/DomainInvalidationTest.php b/src/Tests/Invalidation/DomainInvalidationTest.php deleted file mode 100644 index 386d513..0000000 --- a/src/Tests/Invalidation/DomainInvalidationTest.php +++ /dev/null @@ -1,23 +0,0 @@ -assertTrue($this->service instanceof PluginManagerInterface); - $this->assertTrue($this->service instanceof DefaultPluginManager); - $this->assertTrue($this->service instanceof CachedDiscoveryInterface); - } - - /** - * Test the plugins we expect to be available. - */ - public function testDefinitions() { - $definitions = $this->service->getDefinitions(); - foreach ($this->plugins as $plugin_id) { - $this->assertTrue(isset($definitions[$plugin_id])); - } - foreach ($definitions as $plugin_id => $md) { - $this->assertTrue(in_array($plugin_id, $this->plugins)); - } - foreach ($definitions as $plugin_id => $md) { - foreach ($md as $field => $value) { - $this->assertTrue(in_array($field, $this->annotationFields)); - } - foreach ($this->annotationFields as $field) { - $this->assertTrue(isset($md[$field])); - } - } - } - -} diff --git a/src/Tests/Invalidation/PluginTestBase.php b/src/Tests/Invalidation/PluginTestBase.php deleted file mode 100644 index 473322a..0000000 --- a/src/Tests/Invalidation/PluginTestBase.php +++ /dev/null @@ -1,281 +0,0 @@ -initializeInvalidationFactoryService(); - } - - /** - * Retrieve a invalidation object provided by the plugin. - */ - function getInstance() { - return $this->purgeInvalidationFactory->get( - $this->plugin_id, - $this->expressions[0] - ); - } - - /** - * Retrieve a immutable invalidation object, which wraps the plugin. - */ - function getImmutableInstance() { - return $this->purgeInvalidationFactory->getImmutable( - $this->plugin_id, - $this->expressions[0] - ); - } - - /** - * Tests the code contract strictly enforced on invalidation type plugins. - */ - function testCodeContract() { - $this->assertTrue($this->getInstance() instanceof ImmutableInvalidationInterface); - $this->assertTrue($this->getInstance() instanceof InvalidationInterface); - $this->assertTrue($this->getInstance() instanceof ImmutableInvalidationBase); - $this->assertTrue($this->getInstance() instanceof InvalidationBase); - $this->assertTrue($this->getImmutableInstance() instanceof ImmutableInvalidationInterface); - $this->assertFalse($this->getImmutableInstance() instanceof InvalidationInterface); - $this->assertTrue($this->getImmutableInstance() instanceof ImmutableInvalidationBase); - $this->assertFalse($this->getImmutableInstance() instanceof InvalidationBase); - } - - /** - * Tests \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidation. - */ - function testImmutable() { - $immutable = $this->getImmutableInstance(); - $mutable = $this->getInstance(); - $this->assertEqual($immutable->__toString(), $mutable->__toString()); - $this->assertEqual($immutable->getExpression(), $mutable->getExpression()); - $this->assertEqual($immutable->getState(), $mutable->getState()); - $this->assertEqual($immutable->getStateString(), $mutable->getStateString()); - $this->assertEqual($immutable->getType(), $mutable->getType()); - } - - /** - * Test if setting and getting the object state goes well. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidationInterface::getProperties - * @see \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidationInterface::getProperty - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::deleteProperty - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setProperty - */ - function testProperties() { - $i = $this->getInstance(); - // Verify that getProperties() has the right initial state (of emptyness). - $this->assertEqual($i->getProperties(), []); - // Verify that exceptions are thrown when they should. - $this->assertException('\LogicException', [$i, 'getProperty'], ['key1']); - $this->assertException('\LogicException', [$i, 'setProperty'], ['key1', 'foobar']); - $this->assertNoException('\LogicException', [$i, 'getProperties']); - $i->setStateContext('purger1'); - $this->assertException('\LogicException', [$i, 'getProperties']); - $this->assertNoException('\LogicException', [$i, 'getProperty'], ['key1']); - $this->assertNoException('\LogicException', [$i, 'setProperty'], ['key1', 'foobar']); - // Verify retrieving and setting properties. - $this->assertNull($i->getProperty('doesntexist')); - $this->assertEqual($i->getProperty('key1'), 'foobar'); - $this->assertNull($i->deleteProperty('key1')); - $this->assertNull($i->getProperty('key1')); - $this->assertNull($i->setProperty('key1', 'foobar2')); - $this->assertEqual($i->getProperty('key1'), 'foobar2'); - // Switch state to add some more properties. - $i->setState(InvalidationInterface::FAILED); - $i->setStateContext('purger2'); - $i->setProperty('key2', 'baz'); - $i->setState(InvalidationInterface::FAILED); - $i->setStateContext(NULL); - // Verify that getProperties() works as it should. - $p = $i->getProperties(); - $this->assertEqual(count($p), 2); - $this->assertTrue(isset($p['purger1']['key1'])); - $this->assertEqual($p['purger1']['key1'], 'foobar2'); - $this->assertTrue(isset($p['purger2']['key2'])); - $this->assertEqual($p['purger2']['key2'], 'baz'); - } - - /** - * Test if setting and getting the object state goes well. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getState - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getStateString - */ - function testState() { - $i = $this->getInstance(); - // Test the initial state of the invalidation object. Then verify that a - // BadPluginBehaviorException is thrown when left as FRESH. - $this->assertEqual($i->getState(), InvalidationInterface::FRESH); - $this->assertEqual($i->getStateString(), 'FRESH'); - $i->setStateContext('test'); - $this->assertException('\Drupal\purge\Plugin\Purge\Purger\Exception\BadPluginBehaviorException', [$i, 'setStateContext'], [NULL]); - $i->setState(InvalidationInterface::FAILED); - $i->setStateContext(NULL); - // Verify that setting state in general context throws exceptions. - $this->assertException('\LogicException', [$i, 'setState'], [InvalidationInterface::FAILED]); - // Test \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState catches bad input. - foreach(['2', 'FRESH', -1, 5, 100] as $badstate) { - $this->assertException('\Drupal\purge\Plugin\Purge\Invalidation\Exception\InvalidStateException', [$i, 'setState'], [$badstate]); - } - // Test setting normal states results in the same return state. - $test_states = [ - InvalidationInterface::PROCESSING => 'PROCESSING', - InvalidationInterface::SUCCEEDED => 'SUCCEEDED', - InvalidationInterface::FAILED => 'FAILED', - InvalidationInterface::NOT_SUPPORTED => 'NOT_SUPPORTED', - ]; - $context = 0; - $i->setStateContext((string)$context); - foreach ($test_states as $state => $string) { - $this->assertNull($i->setStateContext((string) ($context++))); - $this->assertNull($i->setState($state)); - $this->assertEqual($i->getState(), $state); - $this->assertEqual($i->getStateString(), $string); - } - $i->setStateContext(NULL); - } - - /** - * Test if typecasting invalidation objects to strings gets us a string. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__toString - */ - function testStringExpression() { - $this->assertEqual( (string)$this->getInstance(), $this->expressions[0], - 'The __toString method returns $expression.'); - } - - /** - * Test if all valid string expressions properly instantiate the object. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__construct - */ - function testValidExpressions() { - if (is_null($this->expressions)) { - $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id); - } - else { - foreach ($this->expressions as $e) { - $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id, $e); - } - } - } - - /** - * Test if all invalid string expressions fail to instantiate the object. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__construct - */ - function testInvalidExpressions($expressions = NULL) { - foreach ($this->expressionsInvalid as $exp) { - $thrown = FALSE; - try { - $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id, $exp); - } - catch (\Exception $e) { - $thrown = $e; - } - if (is_null($exp)) { - $this->assertTrue($thrown instanceof MissingExpressionException, var_export($exp, TRUE)); - } - else { - $this->assertTrue($thrown instanceof InvalidExpressionException, var_export($exp, TRUE)); - } - } - } - - /** - * Test retrieving the plugin ID and definition. - * - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getPluginId - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getType - * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getPluginDefinition - */ - function testPluginIdAndDefinition() { - // Test mutable objects. - $mutable = $this->getInstance(); - $this->assertEqual($this->plugin_id, $mutable->getPluginId()); - $this->assertEqual($this->plugin_id, $mutable->getType()); - $d = $mutable->getPluginDefinition(); - $this->assertTrue(is_array($d)); - $this->assertTrue(is_array($d['examples'])); - $this->assertTrue($d['label'] instanceof TranslatableMarkup); - $this->assertFalse(empty((string) $d['label'])); - $this->assertTrue($d['description'] instanceof TranslatableMarkup); - $this->assertFalse(empty((string) $d['description'])); - $this->assertTrue(isset($d['expression_required'])); - $this->assertTrue(isset($d['expression_can_be_empty'])); - $this->assertTrue(isset($d['expression_must_be_string'])); - if (!$d["expression_required"]) { - $this->assertFalse($d["expression_can_be_empty"]); - } - // Test the immutable objects. - $immutable = $this->getImmutableInstance(); - $this->assertEqual($this->plugin_id, $immutable->getPluginId()); - $this->assertEqual($this->plugin_id, $immutable->getType()); - $d = $immutable->getPluginDefinition(); - $this->assertTrue(is_array($d)); - $this->assertTrue(is_array($d['examples'])); - $this->assertTrue($d['label'] instanceof TranslatableMarkup); - $this->assertFalse(empty((string) $d['label'])); - $this->assertTrue($d['description'] instanceof TranslatableMarkup); - $this->assertFalse(empty((string) $d['description'])); - $this->assertTrue(isset($d['expression_required'])); - $this->assertTrue(isset($d['expression_can_be_empty'])); - $this->assertTrue(isset($d['expression_must_be_string'])); - if (!$d["expression_required"]) { - $this->assertFalse($d["expression_can_be_empty"]); - } - } - -} diff --git a/src/Tests/Invalidation/RegularExpressionInvalidationTest.php b/src/Tests/Invalidation/RegularExpressionInvalidationTest.php deleted file mode 100644 index c5a188a..0000000 --- a/src/Tests/Invalidation/RegularExpressionInvalidationTest.php +++ /dev/null @@ -1,23 +0,0 @@ -initializeService(); + } + + /** + * Initialize the requested service as $this->$variable (or reload). + * + * @param string $variable + * The place to put the loaded/reloaded service, defaults to $this->service. + * @param string $service + * The name of the service to load, defaults to $this->serviceId. + */ + protected function initializeService($variable = 'service', $service = NULL) { + if (is_null($this->$variable)) { + if (is_null($service)) { + $this->$variable = $this->container->get($this->serviceId); + } + else { + $this->$variable = $this->container->get($service); + } + } + if ($this->$variable instanceof ServiceInterface) { + $this->$variable->reload(); + } + } + + /** + * Test for \Drupal\purge\ServiceBase and \Drupal\purge\ServiceInterface. + * + * Services not derived from \Drupal\purge\ServiceInterface, should overload + * this test. This applies to plugin managers for instance. + */ + public function testCodeContract() { + $this->assertTrue($this->service instanceof ServiceInterface); + $this->assertTrue($this->service instanceof ServiceBase); + } + +} diff --git a/tests/src/TestTrait.php b/tests/src/TestTrait.php new file mode 100644 index 0000000..04fefc9 --- /dev/null +++ b/tests/src/TestTrait.php @@ -0,0 +1,272 @@ +assertTrue($thrown, "Exception $exception thrown."); + } + + /** + * Assert that the named exception is thrown. + * + * @param string $exception + * The full name of the exception to be thrown. + * @param callable $call + * The callable to call from within the try statement. + * @param mixed[] $args + * Arguments to be passed to the callable. + */ + protected function assertNoException($exception, callable $call, $args = []) { + $thrown = FALSE; + eval(" + try { + call_user_func_array(\$call, \$args); + } + catch ($exception \$e) { + \$thrown = TRUE; + }"); + $this->assertFalse($thrown, "Exception $exception isn't thrown."); + } + + /** + * Make $this->purgeLogger available. + */ + protected function initializeLoggerService() { + if (is_null($this->purgeLogger)) { + $this->purgeLogger = $this->container->get('purge.logger'); + } + } + + /** + * Make $this->purgeProcessors available. + * + * @param string[] $plugin_ids + * Array of plugin ids to be enabled. + */ + protected function initializeProcessorsService($plugin_ids = []) { + if (is_null($this->purgeProcessors)) { + $this->purgeProcessors = $this->container->get('purge.processors'); + } + if (count($plugin_ids)) { + $this->purgeProcessors->reload(); + $this->purgeProcessors->setPluginsEnabled($plugin_ids); + } + } + + /** + * Make $this->purgePurgers available. + * + * @param string[] $plugin_ids + * Array of plugin ids to be enabled. + */ + protected function initializePurgersService($plugin_ids = []) { + if (is_null($this->purgePurgers)) { + $this->purgePurgers = $this->container->get('purge.purgers'); + } + if (count($plugin_ids)) { + $ids = []; + foreach ($plugin_ids as $i => $plugin_id) { + $ids["id$i"] = $plugin_id; + } + $this->purgePurgers->reload(); + $this->purgePurgers->setPluginsEnabled($ids); + } + } + + /** + * Make $this->purgeInvalidationFactory available. + */ + protected function initializeInvalidationFactoryService() { + if (is_null($this->purgeInvalidationFactory)) { + $this->purgeInvalidationFactory = $this->container->get('purge.invalidation.factory'); + } + } + + /** + * Make $this->purgeQueue available. + * + * @param null|string[] $plugin_id + * The plugin ID of the queue to be configured. + */ + protected function initializeQueueService($plugin_id = NULL) { + if (is_null($this->purgeQueue)) { + $this->purgeQueue = $this->container->get('purge.queue'); + } + if (!is_null($plugin_id)) { + $this->purgeQueue->reload(); + $this->purgeQueue->setPluginsEnabled([$plugin_id]); + } + } + + /** + * Make $this->purgeQueuers available. + * + * @param string[] $plugin_ids + * Array of plugin ids to be enabled. + */ + protected function initializeQueuersService($plugin_ids = []) { + if (is_null($this->purgeQueuers)) { + $this->purgeQueuers = $this->container->get('purge.queuers'); + } + if (count($plugin_ids)) { + $this->purgeQueuers->reload(); + $this->purgeQueuers->setPluginsEnabled($plugin_ids); + } + } + + /** + * Make $this->purgeDiagnostics available. + */ + protected function initializeDiagnosticsService() { + if (is_null($this->purgeDiagnostics)) { + $this->purgeDiagnostics = $this->container->get('purge.diagnostics'); + } + else { + $this->purgeDiagnostics->reload(); + } + } + + /** + * Make $this->purgeTagsheaders available. + */ + protected function initializeTagsHeadersService() { + if (is_null($this->purgeTagsHeaders)) { + $this->purgeTagsHeaders = $this->container->get('purge.tagsheaders'); + } + else { + $this->purgeTagsHeaders->reload(); + } + } + + /** + * Create $number requested invalidation objects. + * + * @param int $number + * The number of objects to generate. + * + * @return array|\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface + * Invalidation object. + */ + public function getInvalidations($number) { + $this->initializeInvalidationFactoryService(); + $set = []; + for ($i = 0; $i < $number; $i++) { + $set[] = $this->purgeInvalidationFactory->get('everything'); + } + return ($number === 1) ? $set[0] : $set; + } + + /** + * Switch to the memory queue backend. + */ + public function setMemoryQueue() { + $this->configFactory = $this->container->get('config.factory'); + $this->config('purge.plugins') + ->set('queue', 'memory') + ->save(); + } + +} diff --git a/tests/src/Unit/Invalidation/DomainInvalidationTest.php b/tests/src/Unit/Invalidation/DomainInvalidationTest.php new file mode 100644 index 0000000..340a22a --- /dev/null +++ b/tests/src/Unit/Invalidation/DomainInvalidationTest.php @@ -0,0 +1,18 @@ +assertTrue($this->service instanceof PluginManagerInterface); + $this->assertTrue($this->service instanceof DefaultPluginManager); + $this->assertTrue($this->service instanceof CachedDiscoveryInterface); + } + + /** + * Test the plugins we expect to be available. + */ + public function testDefinitions() { + $definitions = $this->service->getDefinitions(); + foreach ($this->plugins as $plugin_id) { + $this->assertTrue(isset($definitions[$plugin_id])); + } + foreach ($definitions as $plugin_id => $md) { + $this->assertTrue(in_array($plugin_id, $this->plugins)); + } + foreach ($definitions as $plugin_id => $md) { + foreach ($md as $field => $value) { + $this->assertTrue(in_array($field, $this->annotationFields)); + } + foreach ($this->annotationFields as $field) { + $this->assertTrue(isset($md[$field])); + } + } + } + +} diff --git a/tests/src/Unit/Invalidation/RegularExpressionInvalidationTest.php b/tests/src/Unit/Invalidation/RegularExpressionInvalidationTest.php new file mode 100644 index 0000000..8575301 --- /dev/null +++ b/tests/src/Unit/Invalidation/RegularExpressionInvalidationTest.php @@ -0,0 +1,18 @@ +installConfig(['purge']); + + // The default 'database' queue backend gives issues, switch to 'memory'. + if ($switch_to_memory_queue) { + $this->setMemoryQueue(); + } + } + +} diff --git a/tests/src/Unit/PluginTestBase.php b/tests/src/Unit/PluginTestBase.php new file mode 100644 index 0000000..4f38cae --- /dev/null +++ b/tests/src/Unit/PluginTestBase.php @@ -0,0 +1,275 @@ +initializeInvalidationFactoryService(); + } + + /** + * Retrieve a invalidation object provided by the plugin. + */ + public function getInstance() { + return $this->purgeInvalidationFactory->get( + $this->plugin_id, + $this->expressions[0] + ); + } + + /** + * Retrieve a immutable invalidation object, which wraps the plugin. + */ + public function getImmutableInstance() { + return $this->purgeInvalidationFactory->getImmutable( + $this->plugin_id, + $this->expressions[0] + ); + } + + /** + * Tests the code contract strictly enforced on invalidation type plugins. + */ + public function testCodeContract() { + $this->assertTrue($this->getInstance() instanceof ImmutableInvalidationInterface); + $this->assertTrue($this->getInstance() instanceof InvalidationInterface); + $this->assertTrue($this->getInstance() instanceof ImmutableInvalidationBase); + $this->assertTrue($this->getInstance() instanceof InvalidationBase); + $this->assertTrue($this->getImmutableInstance() instanceof ImmutableInvalidationInterface); + $this->assertFalse($this->getImmutableInstance() instanceof InvalidationInterface); + $this->assertTrue($this->getImmutableInstance() instanceof ImmutableInvalidationBase); + $this->assertFalse($this->getImmutableInstance() instanceof InvalidationBase); + } + + /** + * Tests \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidation. + */ + public function testImmutable() { + $immutable = $this->getImmutableInstance(); + $mutable = $this->getInstance(); + $this->assertEquals($immutable->__toString(), $mutable->__toString()); + $this->assertEquals($immutable->getExpression(), $mutable->getExpression()); + $this->assertEquals($immutable->getState(), $mutable->getState()); + $this->assertEquals($immutable->getStateString(), $mutable->getStateString()); + $this->assertEquals($immutable->getType(), $mutable->getType()); + } + + /** + * Test if setting and getting the object state goes well. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidationInterface::getProperties + * @see \Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidationInterface::getProperty + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::deleteProperty + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setProperty + */ + public function testProperties() { + $i = $this->getInstance(); + // Verify that getProperties() has the right initial state (of emptyness). + $this->assertEquals($i->getProperties(), []); + // Verify that exceptions are thrown when they should. + $this->assertException('\LogicException', [$i, 'getProperty'], ['key1']); + $this->assertException('\LogicException', [$i, 'setProperty'], ['key1', 'foobar']); + $this->assertNoException('\LogicException', [$i, 'getProperties']); + $i->setStateContext('purger1'); + $this->assertException('\LogicException', [$i, 'getProperties']); + $this->assertNoException('\LogicException', [$i, 'getProperty'], ['key1']); + $this->assertNoException('\LogicException', [$i, 'setProperty'], ['key1', 'foobar']); + // Verify retrieving and setting properties. + $this->assertNull($i->getProperty('doesntexist')); + $this->assertEquals($i->getProperty('key1'), 'foobar'); + $this->assertNull($i->deleteProperty('key1')); + $this->assertNull($i->getProperty('key1')); + $this->assertNull($i->setProperty('key1', 'foobar2')); + $this->assertEquals($i->getProperty('key1'), 'foobar2'); + // Switch state to add some more properties. + $i->setState(InvalidationInterface::FAILED); + $i->setStateContext('purger2'); + $i->setProperty('key2', 'baz'); + $i->setState(InvalidationInterface::FAILED); + $i->setStateContext(NULL); + // Verify that getProperties() works as it should. + $p = $i->getProperties(); + $this->assertEquals(count($p), 2); + $this->assertTrue(isset($p['purger1']['key1'])); + $this->assertEquals($p['purger1']['key1'], 'foobar2'); + $this->assertTrue(isset($p['purger2']['key2'])); + $this->assertEquals($p['purger2']['key2'], 'baz'); + } + + /** + * Test if setting and getting the object state goes well. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getState + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getStateString + */ + public function testState() { + $i = $this->getInstance(); + // Test the initial state of the invalidation object. Then verify that a + // BadPluginBehaviorException is thrown when left as FRESH. + $this->assertEquals($i->getState(), InvalidationInterface::FRESH); + $this->assertEquals($i->getStateString(), 'FRESH'); + $i->setStateContext('test'); + $this->assertException('\Drupal\purge\Plugin\Purge\Purger\Exception\BadPluginBehaviorException', [$i, 'setStateContext'], [NULL]); + $i->setState(InvalidationInterface::FAILED); + $i->setStateContext(NULL); + // Verify that setting state in general context throws exceptions. + $this->assertException('\LogicException', [$i, 'setState'], [InvalidationInterface::FAILED]); + // Test \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState catches bad input. + foreach (['2', 'FRESH', -1, 5, 100] as $badstate) { + $this->assertException('\Drupal\purge\Plugin\Purge\Invalidation\Exception\InvalidStateException', [$i, 'setState'], [$badstate]); + } + // Test setting normal states results in the same return state. + $test_states = [ + InvalidationInterface::PROCESSING => 'PROCESSING', + InvalidationInterface::SUCCEEDED => 'SUCCEEDED', + InvalidationInterface::FAILED => 'FAILED', + InvalidationInterface::NOT_SUPPORTED => 'NOT_SUPPORTED', + ]; + $context = 0; + $i->setStateContext((string) $context); + foreach ($test_states as $state => $string) { + $this->assertNull($i->setStateContext((string) ($context++))); + $this->assertNull($i->setState($state)); + $this->assertEquals($i->getState(), $state); + $this->assertEquals($i->getStateString(), $string); + } + $i->setStateContext(NULL); + } + + /** + * Test if typecasting invalidation objects to strings gets us a string. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__toString + */ + public function testStringExpression() { + $this->assertEquals((string) $this->getInstance(), $this->expressions[0], + 'The __toString method returns $expression.'); + } + + /** + * Test if all valid string expressions properly instantiate the object. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__construct + */ + public function testValidExpressions() { + if (is_null($this->expressions)) { + $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id); + } + else { + foreach ($this->expressions as $e) { + $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id, $e); + } + } + } + + /** + * Test if all invalid string expressions fail to instantiate the object. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::__construct + */ + public function testInvalidExpressions($expressions = NULL) { + foreach ($this->expressionsInvalid as $exp) { + $thrown = FALSE; + try { + $invalidation = $this->purgeInvalidationFactory->get($this->plugin_id, $exp); + } + catch (\Exception $e) { + $thrown = $e; + } + if (is_null($exp)) { + $this->assertTrue($thrown instanceof MissingExpressionException, var_export($exp, TRUE)); + } + else { + $this->assertTrue($thrown instanceof InvalidExpressionException, var_export($exp, TRUE)); + } + } + } + + /** + * Test retrieving the plugin ID and definition. + * + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getPluginId + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getType + * @see \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::getPluginDefinition + */ + public function testPluginIdAndDefinition() { + // Test mutable objects. + $mutable = $this->getInstance(); + $this->assertEquals($this->plugin_id, $mutable->getPluginId()); + $this->assertEquals($this->plugin_id, $mutable->getType()); + $d = $mutable->getPluginDefinition(); + $this->assertTrue(is_array($d)); + $this->assertTrue(is_array($d['examples'])); + $this->assertTrue($d['label'] instanceof TranslatableMarkup); + $this->assertFalse(empty((string) $d['label'])); + $this->assertTrue($d['description'] instanceof TranslatableMarkup); + $this->assertFalse(empty((string) $d['description'])); + $this->assertTrue(isset($d['expression_required'])); + $this->assertTrue(isset($d['expression_can_be_empty'])); + $this->assertTrue(isset($d['expression_must_be_string'])); + if (!$d["expression_required"]) { + $this->assertFalse($d["expression_can_be_empty"]); + } + // Test the immutable objects. + $immutable = $this->getImmutableInstance(); + $this->assertEquals($this->plugin_id, $immutable->getPluginId()); + $this->assertEquals($this->plugin_id, $immutable->getType()); + $d = $immutable->getPluginDefinition(); + $this->assertTrue(is_array($d)); + $this->assertTrue(is_array($d['examples'])); + $this->assertTrue($d['label'] instanceof TranslatableMarkup); + $this->assertFalse(empty((string) $d['label'])); + $this->assertTrue($d['description'] instanceof TranslatableMarkup); + $this->assertFalse(empty((string) $d['description'])); + $this->assertTrue(isset($d['expression_required'])); + $this->assertTrue(isset($d['expression_can_be_empty'])); + $this->assertTrue(isset($d['expression_must_be_string'])); + if (!$d["expression_required"]) { + $this->assertFalse($d["expression_can_be_empty"]); + } + } + +}