Version 1
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Tests / SimplesitemapTest.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Class SimplesitemapTest.
9  *
10  * Tests Simple XML sitemap functional integration.
11  *
12  * @package Drupal\simple_sitemap\Tests
13  * @group simple_sitemap
14  */
15 class SimplesitemapTest extends WebTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['simple_sitemap', 'node'];
23
24   protected $generator;
25   protected $node;
26   protected $node2;
27   protected $privilegedUser;
28
29   // Uncomment to disable strict schema checks.
30 //  protected $strictConfigSchema = FALSE;
31
32   /**
33    * Implements setup().
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->drupalCreateContentType(['type' => 'page']);
39     $this->node = $this->createNode(['title' => 'Node', 'type' => 'page']);
40     $this->node2 = $this->createNode(['title' => 'Node2', 'type' => 'page']);
41
42     $perms = array_keys(\Drupal::service('user.permissions')->getPermissions());
43     $this->privilegedUser = $this->drupalCreateUser($perms);
44
45     $this->generator = \Drupal::service('simple_sitemap.generator');
46   }
47
48   /**
49    * Verify sitemap.xml has the link to the front page with priority '1.0' after first generation.
50    */
51   public function testInitialGeneration() {
52     $this->generator->generateSitemap('nobatch');
53     $this->drupalGet('sitemap.xml');
54     $this->assertRaw('urlset');
55     $this->assertText($GLOBALS['base_url']);
56     $this->assertText('1.0');
57   }
58
59   /**
60    *
61    */
62   public function testSetBundleSettings() {
63
64     // Index new bundle.
65     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
66       ->generateSitemap('nobatch');
67
68     $this->drupalGet('sitemap.xml');
69     $this->assertText('node/' . $this->node->id());
70     $this->assertText('0.5');
71
72     // Only change bundle priority.
73     $this->generator->setBundleSettings('node', 'page', ['priority' => 0.9])
74       ->generateSitemap('nobatch');
75
76     $this->drupalGet('sitemap.xml');
77     $this->assertText('node/' . $this->node->id());
78     $this->assertNoText('0.5');
79     $this->assertText('0.9');
80
81     // Set bundle 'index' setting to 0.
82     $this->generator->setBundleSettings('node', 'page', ['index' => 0])
83       ->generateSitemap('nobatch');
84
85     $this->drupalGet('sitemap.xml');
86     $this->assertNoText('node/' . $this->node->id());
87     $this->assertNoText('0.5');
88     $this->assertNoText('0.9');
89   }
90
91   /**
92    * Test cacheability of the response.
93    */
94   public function testCacheability() {
95     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
96       ->generateSitemap('nobatch');
97
98     // Verify the cache was flushed and node is in the sitemap.
99     $this->drupalGet('sitemap.xml');
100     $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
101     $this->assertText('node/' . $this->node->id());
102
103     // Verify the sitemap is taken from cache on second call and node is in the sitemap.
104     $this->drupalGet('sitemap.xml');
105     $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
106     $this->assertText('node/' . $this->node->id());
107   }
108
109   /**
110    * Test overriding of bundle settings for a single entity.
111    */
112   public function testSetEntityInstanceSettings() {
113     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
114       ->setEntityInstanceSettings('node', $this->node->id(), ['index' => TRUE, 'priority' => 0.1])
115       ->generateSitemap('nobatch');
116
117     $this->drupalGet('sitemap.xml');
118     $this->assertText('node/' . $this->node->id());
119     $this->assertText('0.5');
120     $this->assertText('0.1');
121   }
122
123   /**
124    * Test disabling sitemap support for an entity type.
125    */
126   public function testDisableEntityType() {
127     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
128       ->disableEntityType('node');
129
130     $this->drupalLogin($this->privilegedUser);
131     $this->drupalGet('admin/structure/types/manage/page');
132     $this->assertNoText('Simple XML sitemap');
133
134     $this->generator->generateSitemap('nobatch');
135
136     $this->drupalGet('sitemap.xml');
137     $this->assertNoText('node/' . $this->node->id());
138     $this->assertNoText('0.5');
139   }
140
141   /**
142    * Test enabling sitemap support for an entity type.
143    */
144   public function testEnableEntityType() {
145     $this->generator->disableEntityType('node')
146       ->enableEntityType('node')
147       ->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5]);
148
149     $this->drupalLogin($this->privilegedUser);
150     $this->drupalGet('admin/structure/types/manage/page');
151     $this->assertText('Simple XML sitemap');
152
153     $this->generator->generateSitemap('nobatch');
154
155     $this->drupalGet('sitemap.xml');
156     $this->assertText('node/' . $this->node->id());
157     $this->assertText('0.5');
158   }
159
160   /**
161    * Test removing all custom paths from the sitemap settings.
162    */
163   public function testRemoveCustomLinks() {
164     $this->generator->removeCustomLinks()
165       ->generateSitemap('nobatch');
166
167     $this->drupalGet('sitemap.xml');
168     $this->assertNoText($GLOBALS['base_url']);
169   }
170
171   /**
172    * Test sitemap index.
173    */
174   public function testSitemapIndex() {
175     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
176       ->saveSetting('max_links', 1)
177       ->removeCustomLinks()
178       ->generateSitemap('nobatch');
179
180     $this->drupalGet('sitemap.xml');
181     $this->assertText('sitemaps/1/sitemap.xml');
182     $this->assertText('sitemaps/2/sitemap.xml');
183
184     $this->drupalGet('sitemaps/1/sitemap.xml');
185     $this->assertText('node/' . $this->node->id());
186     $this->assertText('0.5');
187
188     $this->drupalGet('sitemaps/2/sitemap.xml');
189     $this->assertText('node/' . $this->node2->id());
190     $this->assertText('0.5');
191   }
192
193   /**
194    * Test setting the base URL.
195    */
196   public function testSetBaseUrl() {
197     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
198       ->saveSetting('base_url', 'http://base_url_test')
199       ->generateSitemap('nobatch');
200
201     $this->drupalGet('sitemap.xml');
202     $this->assertText('http://base_url_test');
203   }
204
205   /**
206    * Test setting the base URL in the sitemap index.
207    */
208   public function testSetBaseUrlInSitemapIndex() {
209     $this->generator->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
210       ->saveSetting('max_links', 1)
211       ->saveSetting('base_url', 'http://base_url_test')
212       ->generateSitemap('nobatch');
213
214     $this->drupalGet('sitemap.xml');
215     $this->assertText('http://base_url_test');
216   }
217
218   /**
219    * Test adding a custom link to the sitemap.
220    */
221   public function testAddCustomLink() {
222     $this->generator->addCustomLink('/node/' . $this->node->id(), ['priority' => 0.2])
223       ->generateSitemap('nobatch');
224
225     $this->drupalGet('sitemap.xml');
226     $this->assertText('node/' . $this->node->id());
227     $this->assertText('0.2');
228   }
229
230   /**
231    * Test removing custom links from the sitemap.
232    */
233   public function testRemoveCustomLink() {
234     $this->generator->addCustomLink('/node/' . $this->node->id(), ['priority' => 0.2])
235       ->removeCustomLink('/node/' . $this->node->id())
236       ->generateSitemap('nobatch');
237
238     $this->drupalGet('sitemap.xml');
239     $this->assertNoText('node/' . $this->node->id());
240     $this->assertNoText('0.2');
241   }
242
243 }