1<?php
2
3namespace dokuwiki\plugin\struct\test\types;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6use dokuwiki\plugin\struct\test\StructTest;
7use dokuwiki\plugin\struct\types\Url;
8
9/**
10 * Testing the Url Type
11 *
12 * @group plugin_struct
13 * @group plugins
14 */
15class UrlTest extends StructTest
16{
17
18    /**
19     * Provides failing validation data
20     *
21     * @return array
22     */
23    public function validateFailProvider()
24    {
25        return [
26            ['foo', '', '', ''],
27            ['http', '', '', ''],
28            ['http://', '', '', ''],
29            ['foo', 'pre', '', ''],
30            ['foo', '', 'post', ''],
31            ['foo', 'pre', 'post', ''],
32
33            ['http://', '', '', 'http']
34        ];
35    }
36
37    /**
38     * Provides successful validation data
39     *
40     * @return array
41     */
42    public function validateSuccessProvider()
43    {
44        return [
45            ['http://www.example.com', '', '', ''],
46            ['www.example.com', 'http://', '', ''],
47            ['www.example.com', 'http://', 'bang', ''],
48            ['http://www.example.com', '', 'bang', ''],
49
50            ['foo', '', '', 'http'],
51            ['http', '', '', 'http'],
52            ['foo', 'pre', '', 'http'],
53            ['foo', '', 'post', 'http'],
54            ['foo', 'pre', 'post', 'http']
55        ];
56    }
57
58    /**
59     * Provide data to test autoshortening feature
60     *
61     * @return array
62     */
63    public function generateAutoTitleProvider()
64    {
65        return [
66            ['https://foobar.com', 'foobar.com'],
67            ['https://foobar.com/', 'foobar.com'],
68            ['https://www.foobar.com/', 'foobar.com'],
69            ['https://www.foobar.com/test', 'foobar.com/…'],
70            ['https://www.foobar.com/?test', 'foobar.com/'],
71            ['https://www.foobar.com/#hash', 'foobar.com/…'],
72        ];
73    }
74
75    /**
76     * @dataProvider validateFailProvider
77     */
78    public function test_validate_fail($value, $prefix, $postfix, $autoscheme)
79    {
80        $this->expectException(ValidationException::class);
81        $url = new Url(['prefix' => $prefix, 'postfix' => $postfix, 'autoscheme' => $autoscheme]);
82        $url->validate($value);
83    }
84
85    /**
86     * @dataProvider validateSuccessProvider
87     */
88    public function test_validate_success($value, $prefix, $postfix, $autoscheme)
89    {
90        $url = new Url(['prefix' => $prefix, 'postfix' => $postfix, 'autoscheme' => $autoscheme]);
91        $url->validate($value);
92        $this->assertTrue(true); // we simply check that no exceptions are thrown
93    }
94
95    /**
96     * @dataProvider generateAutoTitleProvider
97     */
98    public function test_generateAutoTitle($input, $title)
99    {
100        $url = new Url(['autoshorten' => true]);
101        $result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
102        $this->assertSame($title, $result);
103
104        $url = new Url(['autoshorten' => false]);
105        $result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
106        $this->assertSame($input, $result);
107    }
108
109    public function test_generateFixedTitle()
110    {
111        $input = 'https://www.foobar.com/long';
112        $title = 'oink';
113
114        $url = new Url(['fixedtitle' => $title]);
115        $result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
116        $this->assertSame($title, $result);
117    }
118}
119