XmlSitemapTestBase.php 15 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\xmlsitemap\Tests;

5
use Drupal\Core\Logger\RfcLogLevel;
6
use Drupal\Core\Url;
7
use Drupal\simpletest\WebTestBase;
8
use Drupal\xmlsitemap\Entity\XmlSitemap;
9

10
11
12
/**
 * Helper test class with some added functions for testing.
 */
13
abstract class XmlSitemapTestBase extends WebTestBase {
14

15
16
17
18
19
  /**
   * Modules to enable.
   *
   * @var array
   */
amateescu's avatar
amateescu committed
20
  public static $modules = ['node', 'system', 'user', 'xmlsitemap'];
21
22
23
24
25

  /**
   * The admin user account.
   *
   * @var \Drupal\Core\Session\AccountInterface
26
27
   *
   * @codingStandardsIgnoreStart
28
   */
29
  protected $admin_user;
30

31
32
33
34
35
36
  /**
   * The normal user account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $normal_user;
37
  // @codingStandardsIgnoreEnd
38

39
40
41
42
43
  /**
   * The state store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
44
  protected $state;
45
46
47
48
49
50

  /**
   * The xmlsitemap.settings configuration object.
   *
   * @var \Drupal\Core\Config\Config
   */
51
  protected $config;
52
53
54
55
56
57

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
58
  protected $moduleHandler;
59

60
61
62
63
64
65
66
  /**
   * The language manager object.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

67
68
69
70
71
72
73
  /**
   * The xmlsitemap link storage handler.
   *
   * @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface
   */
  protected $linkStorage;

74
75
76
  /**
   * {@inheritdoc}
   */
77
  protected function setUp() {
78
    parent::setUp();
amateescu's avatar
amateescu committed
79

80
    $this->state = \Drupal::state();
amateescu's avatar
amateescu committed
81
    $this->config = \Drupal::configFactory()->getEditable('xmlsitemap.settings');
82
    $this->moduleHandler = \Drupal::moduleHandler();
83
    $this->languageManager = \Drupal::languageManager();
84
    $this->linkStorage = \Drupal::service('xmlsitemap.link_storage');
85
86
87

    // Create the Article and Page content types.
    if ($this->profile != 'standard') {
amateescu's avatar
amateescu committed
88
89
90
91
92
      $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
      ]);
      $this->drupalCreateContentType([
93
94
95
        'type' => 'page',
        'name' => 'Basic page',
        'settings' => [
96
          // Set proper default options for the page content type.
97
98
99
100
          'node' => [
            'options' => ['promote' => FALSE],
            'submitted' => FALSE,
          ],
amateescu's avatar
amateescu committed
101
        ],
102
      ]);
103
    }
104
105
  }

106
107
108
  /**
   * {@inheritdoc}
   */
109
  public function tearDown() {
110
111
112
113
114
115
116
117
118
    // Capture any (remaining) watchdog errors.
    $this->assertNoWatchdogErrors();

    parent::tearDown();
  }

  /**
   * Assert the page does not respond with the specified response code.
   *
119
   * @param string $code
120
121
   *   Response code. For example 200 is a successful page request. For a list
   *   of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
122
   * @param string $message
123
   *   Message to display.
124
125
   * @param string $group
   *   Name of the group.
126
   *
127
   * @return mixed
128
129
   *   Assertion result.
   */
130
  protected function assertNoResponse($code, $message = '', $group = 'Browser') {
131
132
    $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
    $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
133
    return $this->assertFalse($match, $message ? $message : t('HTTP response not expected @code, actual @curl_code', ['@code' => $code, '@curl_code' => $curl_code]), t('Browser'));
134
135
136
137
138
  }

  /**
   * Check the files directory is created (massive fails if not done).
   *
139
   * @todo This can be removed when https://www.drupal.org/node/654752 is fixed.
140
141
142
143
144
145
146
147
148
149
   */
  protected function checkFilesDirectory() {
    if (!xmlsitemap_check_directory()) {
      $this->fail(t('Sitemap directory was found and writable for testing.'));
    }
  }

  /**
   * Retrieves an XML sitemap.
   *
150
   * @param array $context
151
   *   An optional array of the XML sitemap's context.
152
153
154
155
   * @param array $options
   *   Options to be forwarded to Url::fromUri(). These values will be merged
   *   with, but always override $sitemap->uri['options'].
   * @param array $headers
156
157
   *   An array containing additional HTTP request headers, each formatted as
   *   "name: value".
158
   *
159
   * @return string
160
161
   *   The retrieved HTML string, also available as $this->drupalGetContent()
   */
162
  protected function drupalGetSitemap(array $context = [], array $options = [], array $headers = []) {
163
    $sitemap = XmlSitemap::loadByContext($context);
164
165
166
    if (!$sitemap) {
      return $this->fail('Could not load sitemap by context.');
    }
167
168
    $uri = xmlsitemap_sitemap_uri($sitemap);
    return $this->drupalGet($uri['path'], $options + $uri['options'], $headers);
169
170
171
172
173
174
  }

  /**
   * Regenerate the sitemap by setting the regenerate flag and running cron.
   */
  protected function regenerateSitemap() {
175
176
    $this->state->set('xmlsitemap_regenerate_needed', TRUE);
    $this->state->set('xmlsitemap_generated_last', 0);
177
    $this->cronRun();
178
    $this->assertTrue($this->state->get('xmlsitemap_generated_last') && !$this->state->get('xmlsitemap_regenerate_needed'), t('XML sitemaps regenerated and flag cleared.'));
179
180
  }

181
  /**
182
   * Assert Sitemap Link.
183
   */
184
185
  protected function assertSitemapLink($entity_type, $entity_id = NULL) {
    if (is_array($entity_type)) {
186
      $links = $this->linkStorage->loadMultiple($entity_type);
187
188
189
      $link = $links ? reset($links) : FALSE;
    }
    else {
190
      $link = $this->linkStorage->load($entity_type, $entity_id);
191
192
193
194
195
    }
    $this->assertTrue(is_array($link), 'Link loaded.');
    return $link;
  }

196
  /**
197
   * Assert No Sitemap Link.
198
   */
199
200
  protected function assertNoSitemapLink($entity_type, $entity_id = NULL) {
    if (is_array($entity_type)) {
201
      $links = $this->linkStorage->loadMultiple($entity_type);
202
203
204
      $link = $links ? reset($links) : FALSE;
    }
    else {
205
      $link = $this->linkStorage->load($entity_type, $entity_id);
206
207
208
209
210
    }
    $this->assertFalse($link, 'Link not loaded.');
    return $link;
  }

211
  /**
212
   * Assert Sitemap Link Visible.
213
   */
214
  protected function assertSitemapLinkVisible($entity_type, $entity_id) {
215
    $link = $this->linkStorage->load($entity_type, $entity_id);
216
    $this->assertTrue($link && $link['access'] && $link['status'], t('Sitemap link @type @id is visible.', ['@type' => $entity_type, '@id' => $entity_id]));
217
218
  }

219
  /**
220
   * Assert Sitemap Link Not Visible.
221
   */
222
  protected function assertSitemapLinkNotVisible($entity_type, $entity_id) {
223
    $link = $this->linkStorage->load($entity_type, $entity_id);
224
    $this->assertTrue($link && !($link['access'] && $link['status']), t('Sitemap link @type @id is not visible.', ['@type' => $entity_type, '@id' => $entity_id]));
225
226
  }

227
  /**
228
   * Assert Sitemap Link Values.
229
   */
230
  protected function assertSitemapLinkValues($entity_type, $entity_id, array $conditions) {
231
    $link = $this->linkStorage->load($entity_type, $entity_id);
232
233

    if (!$link) {
234
      return $this->fail(t('Could not load sitemap link for @type @id.', ['@type' => $entity_type, '@id' => $entity_id]));
235
236
237
238
239
    }

    foreach ($conditions as $key => $value) {
      if ($value === NULL || $link[$key] === NULL) {
        // For nullable fields, always check for identical values (===).
240
241
242
243
244
        $this->assertIdentical($link[$key], $value, t('Identical values for @type @id link field @key.', [
          '@type' => $entity_type,
          '@id' => $entity_id,
          '@key' => $key,
        ]));
245
246
247
      }
      else {
        // Otherwise check simple equality (==).
248
249
250
251
252
253
254
        $this->assertEqual($link[$key], $value, t('Equal values for @type @id link field @key - @a - @b.', [
          '@type' => $entity_type,
          '@id' => $entity_id,
          '@key' => $key,
          '@a' => $link[$key],
          '@b' => $value,
        ]));
255
256
257
258
      }
    }
  }

259
  /**
260
   * Assert Not Sitemap Link Values.
261
   */
262
  protected function assertNotSitemapLinkValues($entity_type, $entity_id, array $conditions) {
263
    $link = $this->linkStorage->load($entity_type, $entity_id);
264
265

    if (!$link) {
266
      return $this->fail(t('Could not load sitemap link for @type @id.', ['@type' => $entity_type, '@id' => $entity_id]));
267
268
269
270
271
    }

    foreach ($conditions as $key => $value) {
      if ($value === NULL || $link[$key] === NULL) {
        // For nullable fields, always check for identical values (===).
272
273
274
275
276
        $this->assertNotIdentical($link[$key], $value, t('Not identical values for @type @id link field @key.', [
          '@type' => $entity_type,
          '@id' => $entity_id,
          '@key' => $key,
        ]));
277
278
279
      }
      else {
        // Otherwise check simple equality (==).
280
281
282
283
284
        $this->assertNotEqual($link[$key], $value, t('Not equal values for link @type @id field @key.', [
          '@type' => $entity_type,
          '@id' => $entity_id,
          '@key' => $key,
        ]));
285
286
287
288
      }
    }
  }

289
  /**
290
   * Assert Raw Sitemap Links.
291
   */
292
293
294
  protected function assertRawSitemapLinks() {
    $links = func_get_args();
    foreach ($links as $link) {
295
296
      $path = Url::fromUri('internal:' . $link['loc'], ['language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE])->toString();
      $this->assertRaw($link['loc'], t('Link %path found in the sitemap.', ['%path' => $path]));
297
298
299
    }
  }

300
  /**
301
   * Assert No Raw Sitemap Links.
302
   */
303
304
305
  protected function assertNoRawSitemapLinks() {
    $links = func_get_args();
    foreach ($links as $link) {
306
307
      $path = Url::fromUri('internal:' . $link['loc'], ['language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE])->toString();
      $this->assertNoRaw($link['loc'], t('Link %path not found in the sitemap.', ['%path' => $path]));
308
309
310
    }
  }

311
  /**
312
   * Add Sitemap Link.
313
314
   */
  protected function addSitemapLink(array $link = []) {
315
316
    $last_id = &drupal_static(__FUNCTION__, 1);

317
    $link += [
318
      'type' => 'testing',
amateescu's avatar
amateescu committed
319
      'subtype' => '',
320
321
322
      'id' => $last_id,
      'access' => 1,
      'status' => 1,
323
    ];
324
325

    // Make the default path easier to read than a random string.
326
    $link += ['loc' => '/' . $link['type'] . '-' . $link['id']];
327
328

    $last_id = max($last_id, $link['id']) + 1;
329
    $this->linkStorage->save($link);
330
331
332
    return $link;
  }

333
  /**
334
   * Assert Flag.
335
   */
336
337
338
339
  protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
    $value = xmlsitemap_var($variable);

    if ($reset_if_true && $value) {
340
341
      $state_variables = xmlsitemap_state_variables();
      if (isset($state_variables[$variable])) {
342
        $this->state->set($variable, FALSE);
343
344
      }
      else {
345
        $this->config->set($variable, FALSE)->save();
346
      }
347
348
    }

349
    return $this->assertEqual($value, $assert_value, "$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
350
351
  }

352
  /**
353
   * Assert XML Sitemap Problems.
354
   *
355
   * @codingStandardsIgnoreStart
356
   */
357
  protected function assertXMLSitemapProblems($problem_text = FALSE) {
358
    // @codingStandardsIgnoreEnd
359
360
361
    $this->drupalGet('admin/config/search/xmlsitemap');
    $this->assertText(t('One or more problems were detected with your XML sitemap configuration'));
    if ($problem_text) {
362
      $this->clickLink(t('status report'));
363
364
365
366
      $this->assertText($problem_text);
    }
  }

367
  /**
368
   * Assert No XML Sitemap Problems.
369
   *
370
   * @codingStandardsIgnoreStart
371
   */
372
  protected function assertNoXMLSitemapProblems() {
373
    // @codingStandardsIgnoreEnd
374
375
376
377
378
379
380
381
382
    $this->drupalGet('admin/config/search/xmlsitemap');
    $this->assertNoText(t('One or more problems were detected with your XML sitemap configuration'));
  }

  /**
   * Fetch all seen watchdog messages.
   *
   * @todo Add unit tests for this function.
   */
383
384
  protected function getWatchdogMessages(array $conditions = [], $reset = FALSE) {
    static $seen_ids = [];
385

386
    if (!$this->moduleHandler->moduleExists('dblog') || $reset) {
387
388
      $seen_ids = [];
      return [];
389
390
391
    }

    $query = db_select('watchdog');
392
393
394
395
396
397
398
399
    $query->fields('watchdog', [
      'wid',
      'type',
      'severity',
      'message',
      'variables',
      'timestamp',
    ]);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    foreach ($conditions as $field => $value) {
      if ($field == 'variables' && !is_string($value)) {
        $value = serialize($value);
      }
      $query->condition($field, $value);
    }
    if ($seen_ids) {
      $query->condition('wid', $seen_ids, 'NOT IN');
    }
    $query->orderBy('timestamp');
    $messages = $query->execute()->fetchAllAssoc('wid');

    $seen_ids = array_merge($seen_ids, array_keys($messages));
    return $messages;
  }

416
  /**
417
   * Assert Watchdog Message.
418
   */
419
420
421
422
  protected function assertWatchdogMessage(array $conditions, $message = 'Watchdog message found.') {
    $this->assertTrue($this->getWatchdogMessages($conditions), $message);
  }

423
  /**
424
   * Assert No Watchdog Message.
425
   */
426
427
428
429
430
431
432
433
434
  protected function assertNoWatchdogMessage(array $conditions, $message = 'Watchdog message not found.') {
    $this->assertFalse($this->getWatchdogMessages($conditions), $message);
  }

  /**
   * Check that there were no watchdog errors or worse.
   */
  protected function assertNoWatchdogErrors() {
    $messages = $this->getWatchdogMessages();
435
    $verbose = [];
436
437
438

    foreach ($messages as $message) {
      $message->text = $this->formatWatchdogMessage($message);
439
440
441
      if (in_array($message->severity, [
        RfcLogLevel::EMERGENCY, RfcLogLevel::ALERT, RfcLogLevel::CRITICAL, RfcLogLevel::ERROR, RfcLogLevel::WARNING,
      ])) {
442
443
444
445
446
447
448
        $this->fail($message->text);
      }
      $verbose[] = $message->text;
    }

    if ($verbose) {
      array_unshift($verbose, '<h2>Watchdog messages</h2>');
449
      $this->verbose(implode('<br />', $verbose), 'Watchdog messages from test run');
450
451
452
    }

    // Clear the seen watchdog messages since we've failed on any errors.
453
    $this->getWatchdogMessages([], TRUE);
454
455
456
457
458
  }

  /**
   * Format a watchdog message in a one-line summary.
   *
459
   * @param string $message
460
   *   A watchdog message object.
461
   *
462
   * @return string
463
464
465
   *   A string containing the watchdog message's timestamp, severity, type,
   *   and actual message.
   */
466
  private function formatWatchdogMessage($message) {
467
468
469
470
    static $levels;

    if (!isset($levels)) {
      module_load_include('admin.inc', 'dblog');
471
      $levels = RfcLogLevel::getLevels();
472
473
    }

474
    return t('@timestamp - @severity - @type - @message', [
475
476
477
      '@timestamp' => $message->timestamp,
      '@severity' => $levels[$message->severity],
      '@type' => $message->type,
478
        // @codingStandardsIgnoreLine
479
480
        // '@message' => theme_dblog_message(array('event' => $message, 'link' => FALSE)),.
    ]);
481
482
483
484
485
486
487
488
  }

  /**
   * Log verbose message in a text file.
   *
   * This is a copy of DrupalWebTestCase->verbose() but allows a customizable
   * summary message rather than hard-coding 'Verbose message'.
   *
489
   * @param string $verbose_message
490
   *   The verbose message to be stored.
491
   * @param string $message
492
   *   Message to display.
493
   *
494
495
   * @see simpletest_verbose()
   *
496
   * @todo Remove when https://www.drupal.org/node/800426 is fixed.
497
498
   */
  protected function verbose($verbose_message, $message = 'Verbose message') {
499
    if ($id = parent::verbose($verbose_message)) {
500
      $url = file_create_url($this->originalFileDirectory . '/simpletest/verbose/' . get_class($this) . '-' . $id . '.html');
501
      $message_url = Url::fromUri($url, ['attributes' => ['target' => '_blank']]);
502
      $this->error($this->l($message, $message_url), 'User notice');
503
504
505
506
    }
  }

}