Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2317193 | Port "Add an item to a list" action to D8 #102

Merged
merged 1 commit into from
Oct 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/Plugin/Action/DataListItemAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* @file
* Contains \Drupal\rules\Plugin\Action\DataListItemAdd.
*/

namespace Drupal\rules\Plugin\Action;

use Drupal\rules\Engine\RulesActionBase;

/**
* Provides an 'Add list item' action.
*
* @Action(
* id = "rules_list_item_add",
* label = @Translation("Add list item"),
* context = {
* "list" = @ContextDefinition("list",
* label = @Translation("List"),
* description = @Translation("The data list, to which an item is to be added.")
* ),
* "item" = @ContextDefinition("any",
* label = @Translation("Item"),
* description = @Translation("Item to add.")
* ),
* "unique" = @ContextDefinition("boolean",
* label = @Translation("Enforce uniqueness"),
* description = @Translation("Only add the item to the list if it is not yet contained."),
* required = FALSE
* ),
* "pos" = @ContextDefinition("string",
* label = @Translation("Insert position"),
* description = @Translation("Position to insert the item."),
* required = FALSE
* )
* },
* provides = {
* "outputlist" = @ContextDefinition("list",
* label = @Translation("The resulting data list with item added.")
* )
* }
* )
*
* @todo: Add access callback information from Drupal 7?
* @todo: Add group information from Drupal 7?
* @todo: set ContextDefinition restriction
* @todo: Add info alter
*/
class DataListItemAdd extends RulesActionBase {

/**
* {@inheritdoc}
*/
public function summary() {
return $this->t('Add list item');
}

/**
* {@inheritdoc}
*/
public function execute() {
$list = $this->getContextValue('list');
$item = $this->getContextValue('item');
$position = ($this->getContextValue('pos') ? $this->getContextValue('pos') : 'end');
$unique = ($this->getContextValue('unique') ? $this->getContextValue('unique') : FALSE);
// Optionally, only add the list item if it is not yet contained.
if (!((bool) $unique && in_array($item, $list))) {
if ($position === 'start' ) {
array_unshift($list, $item);
}
else {
$list[] = $item;
}
}
$this->setContextValue('list', $list);
}
}
162 changes: 162 additions & 0 deletions tests/src/Integration/Action/DataListItemAddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/**
* @file
* Contains \Drupal\Tests\rules\Integration\Action\DataListItemAddTest.
*/

namespace Drupal\Tests\rules\Integration\Action;

use Drupal\Tests\rules\Integration\RulesIntegrationTestBase;

/**
* @coversDefaultClass \Drupal\rules\Plugin\Action\DataListItemAdd
* @group rules_actions
*/
class DataListItemAddTest extends RulesIntegrationTestBase {

/**
* The action to be tested.
*
* @var \Drupal\rules\Engine\RulesActionInterface
*/
protected $action;

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

$this->action = $this->actionManager->createInstance('rules_list_item_add');
}

/**
* Tests the summary.
*
* @covers ::summary()
*/
public function testSummary() {
$this->assertEquals('Add list item', $this->action->summary());
}

/**
* Tests the action execution with default values.
*
* @covers ::execute()
*/
public function testActionExecutionWithDefaults() {
// Test adding a value at the end (default position).
$list = ['One', 'Two', 'Three'];

$this->action
->setContextValue('list', $list)
->setContextValue('item', 'Four');

$this->action->execute();

// The list should contain four items, with the new item at the end.
$this->assertArrayEquals([
'One',
'Two',
'Three',
'Four'
], $this->action->getContextValue('list'));
}

/**
* Tests the action execution - item append.
*
* @covers ::execute()
*/
public function testActionExecutionItemAppend() {
// Test adding a value at the end.
$list = ['One', 'Two', 'Three'];

$this->action
->setContextValue('list', $list)
->setContextValue('item', 'Four')
->setContextValue('pos', 'end');

$this->action->execute();

// The list should contain four items, with the new item added at the end.
$this->assertArrayEquals([
'One',
'Two',
'Three',
'Four'
], $this->action->getContextValue('list'));
}

/**
* Tests the action execution - item prepend.
*
* @covers ::execute()
*/
public function testActionExecutionItemPrepend() {
// Test adding a value at the start.
$list = ['One', 'Two', 'Three'];

$this->action
->setContextValue('list', $list)
->setContextValue('item', 'Zero')
->setContextValue('pos', 'start');

$this->action->execute();

// The list should contain four items, with the new item added at the start.
$this->assertArrayEquals([
'Zero',
'One',
'Two',
'Three'
], $this->action->getContextValue('list'));
}

/**
* Tests the action execution - enforce unique items.
*
* @covers ::execute()
*/
public function testActionExecutionEnforceUnique() {
// Test unique.
$list = ['One', 'Two', 'Three', 'Four'];

$this->action
->setContextValue('list', $list)
->setContextValue('item', 'Four')
->setContextValue('unique', TRUE);

$this->action->execute();

// The list should remain the same.
$this->assertArrayEquals([
'One',
'Two',
'Three',
'Four'
], $this->action->getContextValue('list'));
}

/**
* Tests the action execution - add non-unique items.
*
* @covers ::execute()
*/
public function testActionExecutionNonUnique() {
// Test non-unique.
$list = ['One', 'Two', 'Three', 'Four'];

$this->action
->setContextValue('list', $list)
->setContextValue('item', 'Four')
->setContextValue('unique', FALSE)
->setContextValue('pos', 'end');

$this->action->execute();

// The list should contain five items, with the new item added at the end.
$this->assertArrayEquals(['One', 'Two', 'Three', 'Four', 'Four'], $this->action->getContextValue('list'));
}
}