SwiftypeSiteTreeCrawlerExtensionTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 70
c 1
b 1
f 0
dl 0
loc 178
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testUrlsToCrawlPublished() 0 39 2
A testUrlsToCrawlCacheCleared() 0 14 1
A testUrlsToCrawlUnpublished() 0 44 2
A testUrlsToCrawlSegmentChanged() 0 49 2
A setUp() 0 8 1
1
<?php
2
3
namespace Ichaber\SSSwiftype\Tests\Extensions;
4
5
use Exception;
6
use Ichaber\SSSwiftype\Extensions\SwiftypeSiteTreeCrawlerExtension;
7
use Ichaber\SSSwiftype\Tests\Fake\SwiftypeSiteTree;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
12
/**
13
 * Class SwiftypeMetaTagContentExtensionTest
14
 *
15
 * @package Ichaber\SSSwiftype\Tests\Extensions
16
 */
17
class SwiftypeSiteTreeCrawlerExtensionTest extends SapphireTest
18
{
19
    /**
20
     * @var string
21
     */
22
    protected static $fixture_file = 'SwiftypeSiteTreeCrawlerExtensionTest.yml';
23
24
    public function setUp(): void
25
    {
26
        parent::setUp();
27
28
        // Make sure that our cache is cleared between tests
29
        /** @var SwiftypeSiteTreeCrawlerExtension $crawlerExtension */
30
        $crawlerExtension = Injector::inst()->get(SwiftypeSiteTreeCrawlerExtension::class);
31
        $crawlerExtension->clearCacheAll();
32
    }
33
34
    /**
35
     * @throws Exception
36
     */
37
    public function testUrlsToCrawlPublished(): void
38
    {
39
        // Set our config to not clear caches after un/publish, so that we can easily fetch the Urls for our test
40
        Config::inst()->update(
41
            SwiftypeSiteTreeCrawlerExtension::class,
42
            'clear_cache_disabled',
43
            true
44
        );
45
46
        /** @var SwiftypeSiteTree $page */
47
        $page = $this->objFromFixture(SwiftypeSiteTree::class, 'page1');
48
49
        // Publish single so that Urls to crawl is populated
50
        $page->publishSingle();
51
        $key = str_replace('\\', '', $page->ClassName . $page->ID);
52
53
        $expectedUrls = [
54
            'localhost/page1/',
55
        ];
56
        $urls = [];
57
58
        // Grab the Urls that we expect to have been collated
59
        $urlsToCrawl = $page->getUrlsToCrawl();
60
61
        // Check that the key exists for our page
62
        $this->assertArrayHasKey($key, $urlsToCrawl);
63
64
        // Grab the Urls that are for our page
65
        $urlsToCrawl = $urlsToCrawl[$key];
66
67
        // Strip out any http/https stuff
68
        foreach ($urlsToCrawl as $urlToCrawl) {
69
            $url = str_replace('http://', '', $urlToCrawl);
70
            $url = str_replace('https://', '', $url);
71
72
            $urls[] = $url;
73
        }
74
75
        $this->assertEquals($expectedUrls, $urls, '', 0.0, 10, true);
76
    }
77
78
    /**
79
     * @throws Exception
80
     */
81
    public function testUrlsToCrawlUnpublished(): void
82
    {
83
        // Set our config to not clear caches after un/publish, so that we can easily fetch the Urls for our test
84
        Config::inst()->update(
85
            SwiftypeSiteTreeCrawlerExtension::class,
86
            'clear_cache_disabled',
87
            true
88
        );
89
90
        /** @var SwiftypeSiteTree $page */
91
        $page = $this->objFromFixture(SwiftypeSiteTree::class, 'page2');
92
93
        // Make sure our page is published before we begin
94
        $page->publishSingle();
95
        $key = str_replace('\\', '', $page->ClassName . $page->ID);
96
97
        // Make sure we don't have any Cache set from the above publishing
98
        $page->flushCache();
99
100
        $page->doUnpublish();
101
102
        $expectedUrls = [
103
            'localhost/page2/',
104
        ];
105
        $urls = [];
106
107
        // Grab the Urls that we expect to have been collated
108
        $urlsToCrawl = $page->getUrlsToCrawl();
109
110
        // Check that the key exists for our page
111
        $this->assertArrayHasKey($key, $urlsToCrawl);
112
113
        // Grab the Urls that are for our page
114
        $urlsToCrawl = $urlsToCrawl[$key];
115
116
        // Strip out any http/https stuff
117
        foreach ($urlsToCrawl as $urlToCrawl) {
118
            $url = str_replace('http://', '', $urlToCrawl);
119
            $url = str_replace('https://', '', $url);
120
121
            $urls[] = $url;
122
        }
123
124
        $this->assertEquals($expectedUrls, $urls, '', 0.0, 10, true);
125
    }
126
127
    /**
128
     * @throws Exception
129
     */
130
    public function testUrlsToCrawlSegmentChanged(): void
131
    {
132
        // Set our config to not clear caches after un/publish, so that we can easily fetch the Urls for our test
133
        Config::inst()->update(
134
            SwiftypeSiteTreeCrawlerExtension::class,
135
            'clear_cache_disabled',
136
            true
137
        );
138
139
        /** @var SwiftypeSiteTree $page */
140
        $page = $this->objFromFixture(SwiftypeSiteTree::class, 'page3');
141
142
        // Make sure our page is published before we begin
143
        $page->publishSingle();
144
        $key = str_replace('\\', '', $page->ClassName . $page->ID);
145
146
        // Make sure our cache is flushed from the above publishing
147
        $page->flushCache();
148
149
        // Update our URL Segment
150
        $page->URLSegment = 'page3Changed';
151
        // Publish single so that Urls to crawl is populated
152
        $page->publishSingle();
153
154
        // We expect two Urls now. One from before the segment change, and one from after it
155
        $expectedUrls = [
156
            'localhost/page3/',
157
            'localhost/page3changed/',
158
        ];
159
        $urls = [];
160
161
        // Grab the Urls that we expect to have been collated
162
        $urlsToCrawl = $page->getUrlsToCrawl();
163
164
        // Check that the key exists for our page
165
        $this->assertArrayHasKey($key, $urlsToCrawl);
166
167
        // Grab the Urls that are for our page
168
        $urlsToCrawl = $urlsToCrawl[$key];
169
170
        // Strip out any http/https stuff
171
        foreach ($urlsToCrawl as $urlToCrawl) {
172
            $url = str_replace('http://', '', $urlToCrawl);
173
            $url = str_replace('https://', '', $url);
174
175
            $urls[] = $url;
176
        }
177
178
        $this->assertEquals($expectedUrls, $urls, '', 0.0, 10, true);
179
    }
180
181
    public function testUrlsToCrawlCacheCleared(): void
182
    {
183
        /** @var SwiftypeSiteTree $page */
184
        $page = $this->objFromFixture(SwiftypeSiteTree::class, 'page1');
185
186
        // Publish single so that Urls to crawl is populated
187
        $page->publishSingle();
188
        $key = str_replace('\\', '', $page->ClassName . $page->ID);
189
190
        // Grab the Urls that we expect to have been collated
191
        $urlsToCrawl = $page->getUrlsToCrawl();
192
193
        // Check that the key exists for our page
194
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
195
    }
196
}
197