Commit 79a21191 authored by voleger's avatar voleger Committed by voleger
Browse files

Issue #2956957 by voleger: Add initial test coverage

parent 0bccc024
......@@ -44,13 +44,29 @@ class QueueWorkerManager extends CoreQueueWorkerManager {
* {@inheritdoc}
*/
public function getDefinitions() {
$definitions = parent::getDefinitions();
$weight = $this->config->get('order');
$weight = empty($weight) ? [] : $weight;
return self::sortDefinitions(parent::getDefinitions(), $this->config->get('order') ?: []);
}
/**
* Reorder Queue worker definitions.
*
* @param array $definitions
* Queue worker definitions.
* @param array $weight
* Weight overrides.
*
* @return array
* Reordered Queue worker definitions.
*/
public static function sortDefinitions(array $definitions, array $weight) {
// Prepare definitions for sorting.
foreach ($definitions as $key => &$definition) {
// Define default weight value or hint defined weight to the int value.
$definition['cron']['weight'] = empty($definition['cron']['weight']) ? 0 : intval($definition['cron']['weight']);
// Check weight value overrides.
$definition['weight'] = empty($weight[$key]) ? $definition['cron']['weight'] : intval($weight[$key]);
}
// Sort definitions by weight element.
uasort($definitions, [SortArray::class, 'sortByWeightElement']);
return $definitions;
}
......
<?php
namespace Drupal\queue_order\Tests\Kernel;
/**
* Class DefinitionsWithModuleTest.
*
* @package Drupal\queue_order\Tests\Kernel
*
* @group queue_order
*/
class DefinitionsWithModuleTest extends DefinitionsWithoutModuleTest {
/**
* {@inheritdoc}
*/
public static $modules = ['queue_order_definition_fixtures', 'queue_order'];
/**
* Test equality of Queue Worker definition order.
*/
public function testOrder() {
$this->assertEquals(
$this->orderedList,
array_keys($this->QueueWorkerManager->getDefinitions()),
$this > t('Order is managed by the module')
);
}
}
<?php
namespace Drupal\queue_order\Tests\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Class DefinitionsWithoutModuleTest.
*
* @package Drupal\queue_order\Tests\Kernel
*
* @group queue_order
*/
class DefinitionsWithoutModuleTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['queue_order_definition_fixtures'];
/**
* Queue Worker Manager service.
*
* @var \Drupal\Core\Queue\QueueWorkerManagerInterface
*/
protected $QueueWorkerManager;
protected $orderedList = [
'queue_order_worker_B',
'queue_order_worker_A',
'queue_order_worker_D',
'queue_order_worker_E',
'queue_order_worker_C',
'queue_order_worker_F',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->QueueWorkerManager = $this->container->get('plugin.manager.queue_worker');
}
/**
* Test equality of Queue Worker definition order.
*/
public function testOrder() {
$this->assertNotEquals(
$this->orderedList,
array_keys($this->QueueWorkerManager->getDefinitions()),
$this > t('Order is managed by the core functionality')
);
}
}
name: Queue Order Definitions Test
type: module
description: 'Provides testing definitions for Queue Order module'
package: Testing
version: 3.x
core: 8.x
\ No newline at end of file
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerA.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_A",
* title="Test worker with '2' position",
* cron={"time" = 60, "weight" = -40,}
* )
*/
class WorkerA extends WorkerBase {}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerB.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_B",
* title="Test worker with '1' position",
* cron={"time" = 60, "weight" = -50,}
* )
*/
class WorkerB extends WorkerBase {}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
/**
* Class WorkerBase.
*
* Base Queue Worker class for test Queue Worker classes.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*/
abstract class WorkerBase extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($data) {
$var = 1 + 1;
}
}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerC.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_C",
* title="Test worker with '5' position",
* cron={"time" = 60, "weight" = -10,}
* )
*/
class WorkerC extends WorkerBase {}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerD.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_D",
* title="Test worker with '3' position",
* cron={"time" = 60, "weight" = -30,}
* )
*/
class WorkerD extends WorkerBase {}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerE.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_E",
* title="Test worker with '4' position",
* cron={"time" = 60, "weight" = -20,}
* )
*/
class WorkerE extends WorkerBase {}
<?php
namespace Drupal\queue_order_definition_fixtures\Plugin\QueueWorker;
use Drupal\Core\Annotation\QueueWorker;
/**
* Class WorkerF.
*
* @package Drupal\queue_order_definition_fixtures\Plugin\QueueWorker
*
* @QueueWorker(
* id="queue_order_worker_F",
* title="Test worker with 'last' position",
* cron={"time" = 60, "weight" = 10,}
* )
*/
class WorkerF extends WorkerBase {}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment