Issues (31)

tests/Extension/IndexingExtensionTest.php (3 issues)

Labels
Severity
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\FreeTextSearch\Tests\Extension;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\Security\Member;
8
use SilverStripe\SiteConfig\SiteConfig;
9
use Suilven\FreeTextSearch\Tests\Mock\Indexer;
10
use Suilven\FreeTextSearch\Tests\Models\FlickrPhoto;
11
use Suilven\FreeTextSearch\Tests\Models\FlickrTag;
12
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
13
14
class IndexingExtensionTest extends SapphireTest
15
{
16
17
    protected static $extra_dataobjects = [
18
        FlickrPhoto::class,
19
        FlickrTag::class,
20
    ];
21
22
    // this will enable immediate indexing
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $config = SiteConfig::current_site_config();
28
        $config->FreeTextSearchIndexingModeInBulk = 0;
29
        $config->write();
30
    }
31
32
33
    public function testIndividualPage(): void
34
    {
35
        Indexer::resetIndexedPayload();
36
37
        // only one job is created per index when there are dirty objects to index.  This is dealt with within the
38
        // queued jobs module, in that the job parameters are identical.  One job will already be present from
39
        // loading of the fixtures.  As such, clear the queue
40
        DB::query('DELETE FROM "QueuedJobDescriptor"');
41
42
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
43
44
        $page = new \Page();
45
        $page->Title = 'Rupert liked playing chess';
46
        $page->Content = '<p>THe black queen had been taken</p>';
47
        $page->write();
48
49
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
50
51
        $payload = Indexer::getIndexedPayload();
52
        $this->assertEquals(1, \sizeof($payload));
0 ignored issues
show
It seems like $payload can also be of type null; however, parameter $value of sizeof() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $this->assertEquals(1, \sizeof(/** @scrutinizer ignore-type */ $payload));
Loading history...
53
        $firstPagePayload = $payload[0];
54
        $this->assertEquals([], $firstPagePayload['flickrphotos']);
55
        $this->assertEquals([], $firstPagePayload['members']);
56
        $this->assertEquals('Rupert liked playing chess', $firstPagePayload['sitetree']['Title']);
57
        $this->assertEquals('Rupert liked playing chess', $firstPagePayload['sitetree']['MenuTitle']);
58
        $this->assertEquals('<p>THe black queen had been taken</p>', $firstPagePayload['sitetree']['Content']);
59
    }
60
61
62
    // same as the previous test, but against a different index - it was previously hardcoded
63
    public function testIndividualPhoto(): void
64
    {
65
        Indexer::resetIndexedPayload();
66
67
        // only one job is created per index when there are dirty objects to index.  This is dealt with within the
68
        // queued jobs module, in that the job parameters are identical.  One job will already be present from
69
        // loading of the fixtures.  As such, clear the queue
70
        DB::query('DELETE FROM "QueuedJobDescriptor"');
71
72
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
73
74
        $photo = new FlickrPhoto();
75
        $photo->Title = 'Rupert liked playing chess';
76
        $photo->Description = '<p>THe black queen had been taken</p>';
77
        $photo->write();
78
79
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
80
81
        $payload = Indexer::getIndexedPayload();
82
        $this->assertEquals(1, \sizeof($payload));
0 ignored issues
show
It seems like $payload can also be of type null; however, parameter $value of sizeof() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $this->assertEquals(1, \sizeof(/** @scrutinizer ignore-type */ $payload));
Loading history...
83
        $firstPagePayload = $payload[0];
84
        $this->assertEquals([], $firstPagePayload['sitetree']);
85
        $this->assertEquals([], $firstPagePayload['members']);
86
        $this->assertEquals('Rupert liked playing chess', $firstPagePayload['flickrphotos']['Title']);
87
        $this->assertEquals('<p>THe black queen had been taken</p>', $firstPagePayload['flickrphotos']['Description']);
88
    }
89
90
91
    // same as the previous test, but against a different index - it was previously hardcoded
92
    public function testIndividualMember(): void
93
    {
94
        Indexer::resetIndexedPayload();
95
96
        // only one job is created per index when there are dirty objects to index.  This is dealt with within the
97
        // queued jobs module, in that the job parameters are identical.  One job will already be present from
98
        // loading of the fixtures.  As such, clear the queue
99
        DB::query('DELETE FROM "QueuedJobDescriptor"');
100
101
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
102
103
        $member = new Member();
104
        $member->FirstName = 'Gordon';
105
        $member->Surname = 'Anderson';
106
        $member->Email = '[email protected]';
107
        $member->write();
108
109
        $this->assertEquals(0, QueuedJobDescriptor::get()->count());
110
111
        $payload = Indexer::getIndexedPayload();
112
        $this->assertEquals(1, \sizeof($payload));
0 ignored issues
show
It seems like $payload can also be of type null; however, parameter $value of sizeof() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        $this->assertEquals(1, \sizeof(/** @scrutinizer ignore-type */ $payload));
Loading history...
113
        $firstPagePayload = $payload[0];
114
        $this->assertEquals([], $firstPagePayload['sitetree']);
115
        $this->assertEquals([], $firstPagePayload['flickrphotos']);
116
        $this->assertEquals('Gordon', $firstPagePayload['members']['FirstName']);
117
        $this->assertEquals('Anderson', $firstPagePayload['members']['Surname']);
118
        $this->assertEquals('[email protected]', $firstPagePayload['members']['Email']);
119
    }
120
121
122
    public function tearDown(): void
123
    {
124
        parent::tearDown();
125
126
        $config = SiteConfig::current_site_config();
127
        $config->FreeTextSearchIndexingModeInBulk = 1;
128
        $config->write();
129
    }
130
}
131