diff --git a/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php b/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php index f745a95..bbd3946 100644 --- a/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php +++ b/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php @@ -8,9 +8,12 @@ namespace Drupal\node\Plugin\Action; use Drupal\Component\Utility\Tags; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Action\ConfigurableActionBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Unpublishes a node containing certain keywords. @@ -21,14 +24,52 @@ * type = "node" * ) */ -class UnpublishByKeywordNode extends ConfigurableActionBase { +class UnpublishByKeywordNode extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + + /* + * The renderer. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** + * Constructs a new AssignOwnerNode action. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $renderer) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('renderer') + ); + } /** * {@inheritdoc} */ public function execute($node = NULL) { $build = node_view($node); - $text = drupal_render($build); + $text = $this->renderer->render($build); + foreach ($this->configuration['keywords'] as $keyword) { if (strpos($text, $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) { $node->setPublished(FALSE); diff --git a/core/modules/node/src/Tests/NodeActionsTest.php b/core/modules/node/src/Tests/NodeActionsTest.php index fd1434e..969fd1d 100644 --- a/core/modules/node/src/Tests/NodeActionsTest.php +++ b/core/modules/node/src/Tests/NodeActionsTest.php @@ -6,19 +6,19 @@ */ namespace Drupal\node\Tests; + +use Drupal\system\Entity\Action; use Drupal\node\Entity\Node; +use Drupal\system\Tests\Entity\EntityUnitTestBase; /** * Tests actions provided by the Node module. + * + * @group node */ -class NodeActionsTest extends NodeTestBase { +class NodeActionsTest extends EntityUnitTestBase { - /** - * Modules to enable. - * - * @var array - */ - public static $modules = array('dblog', 'action'); + public static $modules = ['action', 'node']; public static function getInfo() { return array( @@ -32,12 +32,14 @@ public static function getInfo() { * Tests the unpublish node by keyword action. */ function testNodeUnpublishByKeyword() { - $admin_user = $this->drupalCreateUser(array('access content', 'bypass node access')); - $this->drupalLogin($admin_user); + $admin_user = $this->createUser(array('access content', 'bypass node access')); + //$this->drupalLogin($admin_user); $keyword_1 = $this->randomMachineName(); $keyword_2 = $this->randomMachineName(); - $action = entity_create('action', array( + + /* @var Drupal\Core\Action\ActionInterface $action */ + $action = Action::create( array( 'id' => 'node_unpublish_by_keyword_action', 'label' => $this->randomMachineName(), 'type' => 'node', @@ -48,10 +50,11 @@ function testNodeUnpublishByKeyword() { )); $action->save(); - $node = $this->drupalCreateNode(array( - 'body' => array(array('format' => filter_default_format(), 'value' => $keyword_1)) - )); - $node = Node::load($node->id()); + /* @var Drupal\node\NodeInterface $node */ + $node = Node::create([ + 'body' => array(array('format' => filter_default_format($admin_user), 'value' => $keyword_1)) + ]); + $node->save(); $this->assertTrue($node->status->value == NODE_PUBLISHED, 'The node status was set to published.');