xref: /webtrees/tests/app/Elements/XrefIndividualTest.php (revision c2ed51d13a57743094c11c8fe84befd9d4f158cd)
1*c2ed51d1SGreg Roach<?php
2*c2ed51d1SGreg Roach
3*c2ed51d1SGreg Roach/**
4*c2ed51d1SGreg Roach * webtrees: online genealogy
5*c2ed51d1SGreg Roach * Copyright (C) 2021 webtrees development team
6*c2ed51d1SGreg Roach * This program is free software: you can redistribute it and/or modify
7*c2ed51d1SGreg Roach * it under the terms of the GNU General Public License as published by
8*c2ed51d1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*c2ed51d1SGreg Roach * (at your option) any later version.
10*c2ed51d1SGreg Roach * This program is distributed in the hope that it will be useful,
11*c2ed51d1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*c2ed51d1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*c2ed51d1SGreg Roach * GNU General Public License for more details.
14*c2ed51d1SGreg Roach * You should have received a copy of the GNU General Public License
15*c2ed51d1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*c2ed51d1SGreg Roach */
17*c2ed51d1SGreg Roach
18*c2ed51d1SGreg Roachdeclare(strict_types=1);
19*c2ed51d1SGreg Roach
20*c2ed51d1SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21*c2ed51d1SGreg Roach
22*c2ed51d1SGreg Roachuse DOMDocument;
23*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
24*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
25*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Individual;
26*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Registry;
27*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\TestCase;
28*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
29*c2ed51d1SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30*c2ed51d1SGreg Roach
31*c2ed51d1SGreg Roachuse function app;
32*c2ed51d1SGreg Roach
33*c2ed51d1SGreg Roach/**
34*c2ed51d1SGreg Roach * Test harness for the class XrefIndividual
35*c2ed51d1SGreg Roach *
36*c2ed51d1SGreg Roach * @covers \Fisharebest\Webtrees\Elements\AbstractElement
37*c2ed51d1SGreg Roach * @covers \Fisharebest\Webtrees\Elements\AbstractXrefElement
38*c2ed51d1SGreg Roach * @covers \Fisharebest\Webtrees\Elements\XrefIndividual
39*c2ed51d1SGreg Roach */
40*c2ed51d1SGreg Roachclass XrefIndividualTest extends TestCase
41*c2ed51d1SGreg Roach{
42*c2ed51d1SGreg Roach    /**
43*c2ed51d1SGreg Roach     * @return void
44*c2ed51d1SGreg Roach     */
45*c2ed51d1SGreg Roach    public function testEdit(): void
46*c2ed51d1SGreg Roach    {
47*c2ed51d1SGreg Roach        $element = new XrefIndividual('');
48*c2ed51d1SGreg Roach
49*c2ed51d1SGreg Roach        $tree = $this->createMock(Tree::class);
50*c2ed51d1SGreg Roach
51*c2ed51d1SGreg Roach        $factory = $this->createMock(IndividualFactory::class);
52*c2ed51d1SGreg Roach
53*c2ed51d1SGreg Roach        $factory->expects(self::once())
54*c2ed51d1SGreg Roach            ->method('make')
55*c2ed51d1SGreg Roach            ->willReturn(null);
56*c2ed51d1SGreg Roach
57*c2ed51d1SGreg Roach        Registry::individualFactory($factory);
58*c2ed51d1SGreg Roach
59*c2ed51d1SGreg Roach        $request = $this->createMock(ServerRequestInterface::class);
60*c2ed51d1SGreg Roach
61*c2ed51d1SGreg Roach        app()->instance(ServerRequestInterface::class, $request);
62*c2ed51d1SGreg Roach
63*c2ed51d1SGreg Roach        $html = $element->edit('some-id', 'some-name', '@X123@', $tree);
64*c2ed51d1SGreg Roach        $dom  = new DOMDocument();
65*c2ed51d1SGreg Roach        $dom->loadHTML($html);
66*c2ed51d1SGreg Roach
67*c2ed51d1SGreg Roach        $select_nodes = $dom->getElementsByTagName('select');
68*c2ed51d1SGreg Roach        self::assertEquals(1, $select_nodes->count());
69*c2ed51d1SGreg Roach
70*c2ed51d1SGreg Roach        $option_nodes = $select_nodes[0]->getElementsByTagName('option');
71*c2ed51d1SGreg Roach        self::assertEquals(1, $option_nodes->count());
72*c2ed51d1SGreg Roach    }
73*c2ed51d1SGreg Roach
74*c2ed51d1SGreg Roach    /**
75*c2ed51d1SGreg Roach     * @return void
76*c2ed51d1SGreg Roach     */
77*c2ed51d1SGreg Roach    public function testEscape(): void
78*c2ed51d1SGreg Roach    {
79*c2ed51d1SGreg Roach        $element = new XrefIndividual('');
80*c2ed51d1SGreg Roach
81*c2ed51d1SGreg Roach        self::assertSame('@X123@', $element->escape('@X123@'));
82*c2ed51d1SGreg Roach    }
83*c2ed51d1SGreg Roach
84*c2ed51d1SGreg Roach    /**
85*c2ed51d1SGreg Roach     * @return void
86*c2ed51d1SGreg Roach     */
87*c2ed51d1SGreg Roach    public function testValueXrefLink(): void
88*c2ed51d1SGreg Roach    {
89*c2ed51d1SGreg Roach        $element = new XrefIndividual('');
90*c2ed51d1SGreg Roach
91*c2ed51d1SGreg Roach        $record = $this->createMock(Individual::class);
92*c2ed51d1SGreg Roach
93*c2ed51d1SGreg Roach        $record->expects(self::once())
94*c2ed51d1SGreg Roach            ->method('fullName')
95*c2ed51d1SGreg Roach            ->willReturn('Full Name');
96*c2ed51d1SGreg Roach
97*c2ed51d1SGreg Roach        $record->expects(self::once())
98*c2ed51d1SGreg Roach            ->method('url')
99*c2ed51d1SGreg Roach            ->willReturn('https://url');
100*c2ed51d1SGreg Roach
101*c2ed51d1SGreg Roach        $tree = $this->createMock(Tree::class);
102*c2ed51d1SGreg Roach
103*c2ed51d1SGreg Roach        $factory = $this->createMock(IndividualFactory::class);
104*c2ed51d1SGreg Roach
105*c2ed51d1SGreg Roach        $factory->expects(self::once())
106*c2ed51d1SGreg Roach            ->method('make')
107*c2ed51d1SGreg Roach            ->willReturn($record);
108*c2ed51d1SGreg Roach
109*c2ed51d1SGreg Roach
110*c2ed51d1SGreg Roach        Registry::individualFactory($factory);
111*c2ed51d1SGreg Roach
112*c2ed51d1SGreg Roach        self::assertSame('<a href="https://url">Full Name</a>', $element->value('@X123@', $tree));
113*c2ed51d1SGreg Roach    }
114*c2ed51d1SGreg Roach
115*c2ed51d1SGreg Roach    /**
116*c2ed51d1SGreg Roach     * @return void
117*c2ed51d1SGreg Roach     */
118*c2ed51d1SGreg Roach    public function testValueXrefLinkWithInvalidXref(): void
119*c2ed51d1SGreg Roach    {
120*c2ed51d1SGreg Roach        $element = new XrefIndividual('');
121*c2ed51d1SGreg Roach
122*c2ed51d1SGreg Roach        $tree = $this->createMock(Tree::class);
123*c2ed51d1SGreg Roach
124*c2ed51d1SGreg Roach        self::assertSame('<span class="error">invalid</span>', $element->value('invalid', $tree));
125*c2ed51d1SGreg Roach    }
126*c2ed51d1SGreg Roach
127*c2ed51d1SGreg Roach    /**
128*c2ed51d1SGreg Roach     * @return void
129*c2ed51d1SGreg Roach     */
130*c2ed51d1SGreg Roach    public function testValueXrefLinkWithMissingRecord(): void
131*c2ed51d1SGreg Roach    {
132*c2ed51d1SGreg Roach        $element = new XrefIndividual('');
133*c2ed51d1SGreg Roach
134*c2ed51d1SGreg Roach        $tree = $this->createMock(Tree::class);
135*c2ed51d1SGreg Roach
136*c2ed51d1SGreg Roach        $factory = $this->createMock(IndividualFactory::class);
137*c2ed51d1SGreg Roach
138*c2ed51d1SGreg Roach        $factory->expects(self::once())
139*c2ed51d1SGreg Roach            ->method('make')
140*c2ed51d1SGreg Roach            ->willReturn(null);
141*c2ed51d1SGreg Roach
142*c2ed51d1SGreg Roach        Registry::individualFactory($factory);
143*c2ed51d1SGreg Roach
144*c2ed51d1SGreg Roach        self::assertSame('<span class="error">@X321@</span>', $element->value('@X321@', $tree));
145*c2ed51d1SGreg Roach    }
146*c2ed51d1SGreg Roach}
147