-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestmode.module
124 lines (114 loc) · 4.32 KB
/
testmode.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* @file
* A module used for providing hook implementation used in tests.
*
* Test hooks alter content only if the test mode is enabled ('test_mode'
* variable is set).
* This module should never be enabled in production.
*/
declare(strict_types=1);
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\testmode\Testmode;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\Plugin\views\query\Sql;
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_query_alter().
*/
function testmode_views_query_alter(ViewExecutable $view, QueryPluginBase $query): void {
// Don't alter any non-sql views queries.
if (!$query instanceof Sql && !$query instanceof SearchApiQuery) {
return;
}
// Add cache tag to all views.
// Required to invalidate tags when enabling test mode for already cached
// views.
// Since this module is not for production environment, the views caching
// may be reset more often, so using single views tag for all views is
// sufficient.
$view->element['#cache']['tags'][] = Testmode::CACHE_VIEWS;
$testmode = Testmode::getInstance();
if ($testmode->isTestMode()) {
if (in_array($view->id(), $testmode->getNodeViews())) {
$operator = '=';
$field = 'title';
if ($query instanceof Sql) {
// Add 'title' field to the query if it was not originally provided.
if (!in_array('title', array_keys($query->fields))) {
$query->addField('node_field_data', $field);
}
$field = 'node_field_data.' . $field;
$operator = 'LIKE';
}
$group = $query->setWhereGroup('OR');
foreach ($testmode->getNodePatterns() as $pattern) {
$query->addWhere($group, $field, $pattern, $operator);
}
return;
}
if (in_array($view->id(), $testmode->getTermViews())) {
// Add 'name' field to the query if it was not originally provided.
// @phpstan-ignore-next-line
if (!in_array('name', array_keys($query->fields))) {
$query->addField('taxonomy_term_field_data', 'name');
}
$group = $query->setWhereGroup('OR');
foreach ($testmode->getTermPatterns() as $pattern) {
$query->addWhere($group, 'taxonomy_term_field_data.name', $pattern, 'LIKE');
}
return;
}
if (in_array($view->id(), $testmode->getUserViews())) {
// Add 'mail' field to the query if it was not originally provided.
// @phpstan-ignore-next-line
if (!in_array('mail', array_keys($query->fields))) {
$query->addField('users_field_data', 'mail');
}
$group = $query->setWhereGroup('OR');
foreach ($testmode->getUserPatterns() as $pattern) {
$query->addWhere($group, 'users_field_data.mail', $pattern, 'LIKE');
}
return;
}
}
}
/**
* Implements hook_query_alter().
*/
function testmode_query_alter(AlterableInterface $query): void {
// Filter term query used for Taxonomy Term Overview page.
if ($query->hasTag('taxonomy_term_access')) {
$route = Drupal::routeMatch()->getRouteName();
if ($route == 'entity.taxonomy_vocabulary.overview_form') {
$testmode = Testmode::getInstance();
if ($testmode->isTestMode()) {
if ($testmode->getListTerm()) {
/** @var \Drupal\Core\Database\Query\SelectInterface $query */
$query->join('taxonomy_term_field_data', 'ttfd', 'ttfd.tid = t.tid');
$or = $query->orConditionGroup();
foreach ($testmode->getTermPatterns() as $pattern) {
$or->condition('ttfd.name', $pattern, 'LIKE');
}
$query->condition($or);
}
}
}
}
}
/**
* Implements hook_page_bottom().
*
* @phpstan-ignore-next-line
*/
function testmode_page_bottom(array &$page_bottom): void {
if (\Drupal::currentUser()->hasPermission('administer site configuration') && Testmode::getInstance()->isTestMode()) {
$messenger = \Drupal::messenger();
$link = Link::fromTextAndUrl(t('Testmode config page'), Url::fromRoute('testmode.admin_settings'))->toString();
$messenger->addMessage(t('Test mode is enabled. Most likely your tests did not clear the test mode flag properly. Disable test mode at @link.', ['@link' => $link]), MessengerInterface::TYPE_WARNING);
}
}