2fa2194ab9743772b9b309f80adb80279d3ea0a1
[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 overriden.
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 should get a new alias,
115     // except the node above whose alias is manually set. Other aliases must be left alone.
116     $this->patterns['node']->delete();
117     $this->patterns['node'] = $this->createPattern('node', '/archive/node-[node:nid]');
118
119     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
120     $this->assertText('Generated 5 URL aliases.');
121
122     // Prevent existing aliases to be overriden. The bulk generate page should only offer
123     // to create an alias for paths which have none.
124     $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW], t('Save configuration'));
125
126     $this->drupalGet('admin/config/search/path/update_bulk');
127     $this->assertFieldByName('action', 'create');
128     $this->assertText('Pathauto settings are set to ignore paths which already have a URL alias.');
129     $this->assertNoFieldByName('action', 'update');
130     $this->assertNoFieldByName('action', 'all');
131   }
132
133   /**
134    * Tests alias generation for nodes that existed before installing Pathauto.
135    */
136   function testBulkUpdateExistingContent() {
137     // Create a node.
138     $node = $this->drupalCreateNode();
139
140     // Delete its alias and Pathauto metadata.
141     \Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($node);
142     $node->path->first()->get('pathauto')->purge();
143     \Drupal::entityTypeManager()->getStorage('node')->resetCache(array($node->id()));
144
145     // Execute bulk generation.
146     // Bulk create aliases.
147     $edit = array(
148       'update[canonical_entities:node]' => TRUE,
149     );
150     $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
151
152     // Verify that the alias was created for the node.
153     $this->assertText('Generated 1 URL alias.');
154   }
155
156 }