Skip to content
Snippets Groups Projects
Commit cc331f1e authored by sayan goswami's avatar sayan goswami Committed by Adam Balsam
Browse files

Issue #3441497 by secretsayan: Remove redundant node save action before generating recommendations

parent 616ff686
No related branches found
No related tags found
1 merge request!13Remove redundant code
Pipeline #160814 passed with warnings
...@@ -311,12 +311,7 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for ...@@ -311,12 +311,7 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for
$nid = $form_state->getValue('nid') ?? ''; $nid = $form_state->getValue('nid') ?? '';
$node = $form_state->getFormObject()->getEntity(); $node = $form_state->getFormObject()->getEntity();
// Save node. if (empty($nid)) {
if (!empty($nid)) {
$node->save();
}
// Node preview.
else {
$node->in_preview = TRUE; $node->in_preview = TRUE;
} }
...@@ -333,8 +328,8 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for ...@@ -333,8 +328,8 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for
if (!empty($request_id_default[$safe_topic_value])) { if (!empty($request_id_default[$safe_topic_value])) {
$request_id = $request_id_default[$safe_topic_value]['id']; $request_id = $request_id_default[$safe_topic_value]['id'];
} }
// Request new request_id per generation.
else { else {
// Request new request_id per generation.
$request_id = \Drupal::service('conductor.helper')->fetchRequestId( $request_id = \Drupal::service('conductor.helper')->fetchRequestId(
$account_id, $account_id,
$web_property_id, $web_property_id,
...@@ -357,7 +352,7 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for ...@@ -357,7 +352,7 @@ function conductor_generate_recommendations(array $form, FormStateInterface $for
$request_id, $request_id,
); );
} }
catch (\Throwable $e) { catch (\Exception $e) {
$response->addCommand( $response->addCommand(
new HtmlCommand( new HtmlCommand(
'.conductor__message', '.conductor__message',
......
<?php
namespace Drupal\Tests\conductor\Kernel;
use Drupal\conductor\Service\ConductorHelper;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests that a node is not saved under certain conditions.
*
* @group my_module
*/
class ConductorGenerateRecommendationsTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'user',
'system',
'field',
'text',
'filter',
'conductor',
];
/**
* Sets up the environment for the test.
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
$this->installConfig(['node']);
// Create a content type.
NodeType::create(['type' => 'article'])->save();
// Conductor Helper.
$conductor_helper = $this->createMock(ConductorHelper::class);
// Check that the buildPreview method is called.
$conductor_helper->expects($this->once())
->method('buildPreview')
->willReturn('New Article');
// Check that the fetchRequestId method is called.
$conductor_helper->expects($this->once())
->method('fetchRequestId')
->withAnyParameters()
->willReturn('123');
// Check that the fetchRecommendations method is called.
$conductor_helper->expects($this->once())
->method('fetchRecommendations')
->willReturn([]);
$this->container->set('conductor.helper', $conductor_helper);
// Conductor Config.
$this->container->get('config.factory')->getEditable('conductor.settings')
->set('conductor.rank_sources', '')->save();
}
/**
* Tests node is not saved when generating recommendations for new nodes.
*
* @throws \Exception
*/
public function testNodeNotSavedWhenGeneratingRecommendationsForNewNodes(): void {
// Apply the conditions, here we just prepare a node.
$node = Node::create([
'type' => 'article',
'title' => 'New Article',
]);
$node->setNewRevision();
$form = [];
$form_object = $this->createMock(EntityForm::class);
$form_object->expects($this->once())
->method('getEntity')
->willReturn($node);
$form_state = $this->createMock(FormStateInterface::class);
$form_state->expects($this->once())
->method('getFormObject')
->willReturn($form_object);
$form_state->expects($this->atLeastOnce())
->method('getValue')
->willReturn(NULL, 'Sample topic', [], [], 'Sample topic');
conductor_generate_recommendations($form, $form_state);
$node_revisions_count = \Drupal::entityQuery('node')
->condition('title', 'New Article')
->allRevisions()
->count()
->accessCheck(TRUE)
->execute();
$this->assertEquals(0, $node_revisions_count, 'No nodes with the title "New Article" were found.');
}
/**
* Tests node is not saved when generating recommendations for existing nodes.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testNodeNotSavedWhenGeneratingRecommendationsForExistingNodes(): void {
// Apply the conditions, here we just prepare a node.
$node = Node::create([
'type' => 'article',
'title' => 'New Article',
]);
$node->save();
// Update the node and run analysis.
$node->setTitle('New Updated Title');
$node->setNewRevision();
$form = [];
$form_object = $this->createMock(EntityForm::class);
$form_object->expects($this->once())
->method('getEntity')
->willReturn($node);
$form_state = $this->createMock(FormStateInterface::class);
$form_state->expects($this->once())
->method('getFormObject')
->willReturn($form_object);
$form_state->expects($this->atLeastOnce())
->method('getValue')
->willReturn(1, 'Sample topic', [], [], 'Sample topic');
conductor_generate_recommendations($form, $form_state);
$node_revisions_count = \Drupal::entityQuery('node')
->condition('uuid', $node->uuid())
->allRevisions()
->count()
->accessCheck(TRUE)
->execute();
$this->assertEquals(1, $node_revisions_count, 'Only 1 node revision were found.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment