/** * Tests that auto saving in a component executed as action works. */ public function testComponentActionAutoSave() { $entity_type_manager = $this->container->get('entity_type.manager'); $entity_type_manager->getStorage('node_type')->create(['type' => 'page'])->save(); $nested_rule = $this->expressionManager->createRule(); // Create a node entity with the action. $nested_rule->addAction('rules_entity_create:node', ContextConfig::create()->setValue('type', 'page')); // Set the title of the new node so that it is marked for auto-saving. $nested_rule->addAction('rules_data_set', ContextConfig::create()->map('data', 'entity.title')->setValue('value', 'new title')); $rules_config = new RulesComponentConfig(['id' => 'test_rule', 'label' => 'Test rule'], 'rules_component'); $rules_config->setExpression($nested_rule); $rules_config->save(); // Invoke the rules component in another rule. $rule = $this->expressionManager->createRule(); $rule->addAction('rules_component:test_rule'); RulesComponent::create($rule)->execute(); $nodes = Node::loadMultiple(); $node = reset($nodes); $this->assertEquals('new title', $node->getTitle()); $this->assertNotNull($node->id(), 'Node ID is set, which means that the node has been auto-saved.'); }
/** * Tests that auto saving is only triggered once with nested components. */ public function testAutosaveOnlyOnce() { $entity = $this->prophesizeEntity(EntityInterface::class); $nested_rule = $this->rulesExpressionManager->createRule(); $nested_rule->addAction('rules_entity_save', ContextConfig::create()->map('entity', 'entity')); $rules_config = new RulesComponentConfig(['id' => 'test_rule', 'label' => 'Test rule'], 'rules_component'); $rules_config->setExpression($nested_rule); $rules_config->setContextDefinitions(['entity' => ContextDefinition::create('entity')]); $this->prophesizeStorage([$rules_config]); // Create a rule with a nested rule. Overall there are 2 actions to set the // entity then. $rule = $this->rulesExpressionManager->createRule(); $rule->addAction('rules_component:test_rule', ContextConfig::create()->map('entity', 'entity')); $rule->addAction('rules_entity_save', ContextConfig::create()->map('entity', 'entity')); // Auto-saving should only be triggered once on the entity. $entity->save()->shouldBeCalledTimes(1); RulesComponent::create($rule)->addContextDefinition('entity', ContextDefinition::create('entity'))->setContextValue('entity', $entity->reveal())->execute(); }