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