Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoBulkUpdateTest.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
5 use Drupal\pathauto\PathautoGeneratorInterface;
6 use Drupal\pathauto\PathautoState;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Bulk update functionality tests.
11  *
12  * @group pathauto
13  */
14 class PathautoBulkUpdateTest extends WebTestBase {
15
16   use PathautoTestHelperTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = array('node', 'pathauto', 'forum');
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * The created nodes.
34    *
35    * @var \Drupal\node\NodeInterface
36    */
37   protected $nodes;
38
39   /**
40    * The created patterns.
41    *
42    * @var \Drupal\pathauto\PathautoPatternInterface
43    */
44   protected $patterns;
45
46   /**
47    * {@inheritdoc}
48    */
49   function setUp() {
50     parent::setUp();
51
52     // Allow other modules to add additional permissions for the admin user.
53     $permissions = array(
54       'administer pathauto',
55       'administer url aliases',
56       'create url aliases',
57       'administer forums',
58     );
59     $this->adminUser = $this->drupalCreateUser($permissions);
60     $this->drupalLogin($this->adminUser);
61
62     $this->patterns = array();
63     $this->patterns['node'] = $this->createPattern('node', '/content/[node:title]');
64     $this->patterns['user'] = $this->createPattern('user', '/users/[user:name]');
65     $this->patterns['forum'] = $this->createPattern('forum', '/forums/[term:name]');
66   }
67
68   function testBulkUpdate() {
69     // Create some nodes.
70     $this->nodes = array();
71     for ($i = 1; $i <= 5; $i++) {
72       $node = $this->drupalCreateNode();
73       $this->nodes[$node->id()] = $node;
74     }
75
76     // Clear out all aliases.
77     $this->deleteAllAliases();
78
79     // Bulk create aliases.
80     $edit = array(
81       'update[canonical_entities:node]' => TRUE,
82       'update[canonical_entities:user]' => TRUE,
83       'update[forum]' => TRUE,
84     );
85     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
86
87     // This has generated 8 aliases: 5 nodes, 2 users and 1 forum.
88     $this->assertText('Generated 8 URL aliases.');
89
90     // Check that aliases have actually been created.
91     foreach ($this->nodes as $node) {
92       $this->assertEntityAliasExists($node);
93     }
94     $this->assertEntityAliasExists($this->adminUser);
95     // This is the default "General discussion" forum.
96     $this->assertAliasExists(['source' => '/taxonomy/term/1']);
97
98     // Add a new node.
99     $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => PathautoState::SKIP)));
100
101     // Run the update again which should not run against any nodes.
102     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
103     $this->assertText('No new URL aliases to generate.');
104     $this->assertNoEntityAliasExists($new_node);
105
106     // Make sure existing aliases can be overridden.
107     $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_DELETE], t('Save configuration'));
108
109     // Patterns did not change, so no aliases should be regenerated.
110     $edit['action'] = 'all';
111     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
112     $this->assertText('No new URL aliases to generate.');
113
114     // Update the node pattern, and leave other patterns alone. Existing nodes
115     // should get a new alias, except the node above whose alias is manually
116     // set. Other aliases must be left alone.
117     $this->patterns['node']->delete();
118     $this->patterns['node'] = $this->createPattern('node', '/archive/node-[node:nid]');
119
120     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
121     $this->assertText('Generated 5 URL aliases.');
122
123     // Prevent existing aliases to be overridden. The bulk generate page should
124     // only offer to create an alias for paths which have none.
125     $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW], t('Save configuration'));
126
127     $this->drupalGet('admin/config/search/path/update_bulk');
128     $this->assertFieldByName('action', 'create');
129     $this->assertText('Pathauto settings are set to ignore paths which already have a URL alias.');
130     $this->assertNoFieldByName('action', 'update');
131     $this->assertNoFieldByName('action', 'all');
132   }
133
134   /**
135    * Tests alias generation for nodes that existed before installing Pathauto.
136    */
137   function testBulkUpdateExistingContent() {
138     // Create a node.
139     $node = $this->drupalCreateNode();
140
141     // Delete its alias and Pathauto metadata.
142     \Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($node);
143     $node->path->first()->get('pathauto')->purge();
144     \Drupal::entityTypeManager()->getStorage('node')->resetCache(array($node->id()));
145
146     // Execute bulk generation.
147     // Bulk create aliases.
148     $edit = array(
149       'update[canonical_entities:node]' => TRUE,
150     );
151     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
152
153     // Verify that the alias was created for the node.
154     $this->assertText('Generated 1 URL alias.');
155   }
156
157 }