Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
conductor
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
conductor
Commits
cc331f1e
Commit
cc331f1e
authored
1 year ago
by
sayan goswami
Committed by
Adam Balsam
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
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
!13
Remove redundant code
Pipeline
#160814
passed with warnings
1 year ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
conductor.module
+3
-8
3 additions, 8 deletions
conductor.module
tests/src/Kernel/ConductorGenerateRecommendationsTest.php
+158
-0
158 additions, 0 deletions
tests/src/Kernel/ConductorGenerateRecommendationsTest.php
with
161 additions
and
8 deletions
conductor.module
+
3
−
8
View file @
cc331f1e
...
@@ -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'
,
...
...
This diff is collapsed.
Click to expand it.
tests/src/Kernel/ConductorGenerateRecommendationsTest.php
0 → 100644
+
158
−
0
View file @
cc331f1e
<?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.'
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment