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