1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5require_once(__DIR__ . '/../helper/plan.php');
6
7/**
8 * Test cases for the move plugin
9 *
10 * @group plugin_move
11 * @group plugins
12 */
13class plugin_move_plan_test extends DokuWikiTest {
14
15    /**
16     * Create some page namespace structure
17     */
18    function setUp():void {
19        $pages = array(
20            'animals:mammals:bear:brownbear',
21            'animals:mammals:bear:blackbear',
22            'animals:mammals:cute:otter',
23            'animals:mammals:cute:cat',
24            'animals:mammals:cute:dog',
25            'animals:insects:butterfly:fly',
26            'animals:insects:butterfly:moth',
27            'animals:monkey',
28            'humans:programmers:andi',
29            'humans:programmers:joe',
30            'humans:programmers:john',
31            'yeti'
32        );
33        foreach($pages as $page) {
34            saveWikiText($page, $page, 'test setup');
35        }
36
37        parent::setUp();
38    }
39
40    /**
41     * Check that the plan is sorted into the right order
42     */
43    function test_sorting() {
44        $plan = new test_helper_plugin_move_plan();
45
46        $plan->addPageNamespaceMove('animals:mammals:bear', 'animals:mammals:cute:bear');
47        $plan->addPageNamespaceMove('humans:programmers', 'animals:mammals:cute:programmers');
48        $plan->addPageMove('humans:programmers:andi', 'animals:insects:butterfly:andi');
49        $plan->addPageMove('yeti', 'humans:yeti');
50        $plan->addPageMove('animals:monkey', 'monkey');
51
52        $sorted = $plan->sortedPlan();
53
54        // the plan is sorted FORWARD (first things first)
55        $this->assertEquals(5, count($sorted));
56        $this->assertEquals('humans:programmers:andi', $sorted[0]['src']);
57        $this->assertEquals('animals:monkey', $sorted[1]['src']);
58        $this->assertEquals('yeti', $sorted[2]['src']);
59        $this->assertEquals('animals:mammals:bear', $sorted[3]['src']);
60        $this->assertEquals('humans:programmers', $sorted[4]['src']);
61    }
62
63    /**
64     * Move a page out of a namespace and then move the namespace elsewhere
65     */
66    function test_pageinnamespace() {
67        $plan = new test_helper_plugin_move_plan();
68
69        $plan->addPageNamespaceMove('animals:mammals:cute', 'animals:mammals:funny');
70        $plan->addPageMove('animals:mammals:cute:otter', 'animals:mammals:otter');
71
72        $plan->commit();
73        $list = $plan->getList('pagelist');
74
75        // the files are sorted BACKWARDS (first things last)
76        $this->assertEquals(3, count($list));
77        $this->assertEquals("animals:mammals:cute:otter\tanimals:mammals:otter", trim($list[2]));
78        $this->assertEquals("animals:mammals:cute:cat\tanimals:mammals:funny:cat", trim($list[1]));
79        $this->assertEquals("animals:mammals:cute:dog\tanimals:mammals:funny:dog", trim($list[0]));
80
81    }
82}
83
84/**
85 * Class test_helper_plugin_move_plan
86 *
87 * gives access to some internal stuff of the class
88 */
89class test_helper_plugin_move_plan extends helper_plugin_move_plan {
90
91    /**
92     * Access the sorted plan
93     *
94     * @return array
95     */
96    function sortedPlan() {
97        usort($this->plan, array($this, 'planSorter'));
98        return $this->plan;
99    }
100
101    /**
102     * Get the full saved list specified by name
103     *
104     * @param $name
105     * @return array
106     */
107    function getList($name) {
108        return file($this->files[$name]);
109    }
110}