summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Search/ItemSearchProviderTest.php
blob: 062d52a777898fe4bc5a13e0889200fc2139131c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace OCA\News\Search;

use OCA\News\Db\Item;
use OCA\News\Db\ListType;
use OCA\News\Service\ItemServiceV2;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ItemSearchProviderTest extends TestCase
{

    /**
     * @var MockObject|ItemServiceV2
     */
    private $itemService;

    /**
     * @var MockObject|IL10N
     */
    private $l10n;

    /**
     * @var MockObject|IURLGenerator
     */
    private $generator;

    /**
     * @var ItemSearchProvider
     */
    private $class;

    protected function setUp(): void
    {
        $this->l10n = $this->getMockBuilder(IL10N::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->generator = $this->getMockBuilder(IURLGenerator::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->itemService = $this->getMockBuilder(ItemServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->class = new ItemSearchProvider(
            $this->l10n,
            $this->generator,
            $this->itemService
        );
    }

    public function testGetId()
    {
        $this->assertSame('news_item', $this->class->getId());
    }

    public function testGetName()
    {
        $this->l10n->expects($this->once())
                   ->method('t')
                   ->with('News articles')
                   ->willReturnArgument(0);

        $this->assertSame('News articles', $this->class->getName());
    }

    public function testGetOrderExternal()
    {
        $this->assertSame(65, $this->class->getOrder('contacts.Page.index', []));
    }

    public function testGetOrderInternal()
    {
        $this->assertSame(1, $this->class->getOrder('news.page.index', []));
    }

    public function testSearch()
    {
        $user = $this->getMockBuilder(IUser::class)
                     ->getMock();
        $query = $this->getMockBuilder(ISearchQuery::class)
                     ->getMock();

        $query->expects($this->once())
                ->method('getCursor')
                ->willReturn(null);
        
        $query->expects($this->once())
                ->method('getLimit')
                ->willReturn(10);
        
        $user->expects($this->once())
                ->method('getUID')
                ->willReturn('user');

        $query->expects($this->once())
              ->method('getTerm')
              ->willReturn('some text');
        

        $items = [
            Item::fromRow(['id' => 1,'title' => 'some_tErm', 'body' => 'some text', 'feedId' => 1]),
            Item::fromRow(['id' => 2,'title' => 'nothing', 'body' => 'some text', 'feedId' => 1])
        ];

        $this->itemService->expects($this->once())
                            ->method('findAllWithFilters')
                            ->with(
                                'user',
                                ListType::ALL_ITEMS,
                                10,
                                0,
                                false,
                                ['some text'])
                            ->willReturn($items);


        $this->l10n->expects($this->once())
            ->method('t')
            ->with('News')
            ->willReturnArgument(0);

        $this->generator->expects($this->once())
                        ->method('imagePath')
                        ->with('core', 'filetypes/text.svg')
                        ->willReturn('folderpath.svg');

        $this->generator->expects($this->exactly(2))
                        ->method('linkToRoute')
                        ->with('news.page.index')
                        ->willReturn('/news');


        $result = $this->class->search($user, $query)->jsonSerialize();
        $entry = $result['entries'][0]->jsonSerialize();
        $this->assertSame('News', $result['name']);
        $this->assertSame('some_tErm', $entry['title']);
        $this->assertSame('folderpath.svg', $entry['thumbnailUrl']);
        $this->assertSame('some text', $entry['subline']);
        $this->assertSame('/news#/items/feeds/1', $entry['resourceUrl']);
    }
}