MDL-59030 analytics: Social breadth accepting more than level 2
[moodle.git] / analytics / tests / dataset_manager_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Unit tests for the dataset manager.
19  *
20  * @package   core_analytics
21  * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Unit tests for the dataset manager.
29  *
30  * @package   core_analytics
31  * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
32  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
34 class dataset_manager_testcase extends advanced_testcase {
36     /**
37      * test_create_dataset
38      *
39      * @return
40      */
41     public function test_create_dataset() {
42         $this->resetAfterTest(true);
44         $sharedtoprows = array(
45             array('var1', 'var2'),
46             array('value1', 'value2'),
47             array('header1', 'header2')
48         );
50         $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
51         $dataset1->init_process();
52         $dataset1data = array_merge($sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
53         $f1 = $dataset1->store($dataset1data);
54         $dataset1->close_process();
56         $f1contents = $f1->get_content();
57         $this->assertContains('yeah', $f1contents);
58         $this->assertContains('var1', $f1contents);
59         $this->assertContains('value1', $f1contents);
60         $this->assertContains('header1', $f1contents);
61     }
63     /**
64      * test_merge_datasets
65      *
66      * @return
67      */
68     public function test_merge_datasets() {
69         $this->resetAfterTest(true);
71         $sharedtoprows = array(
72             array('var1', 'var2'),
73             array('value1', 'value2'),
74             array('header1', 'header2')
75         );
77         $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
78         $dataset1->init_process();
79         $dataset1data = array_merge($sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
80         $f1 = $dataset1->store($dataset1data);
81         $dataset1->close_process();
83         $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
84         $dataset2->init_process();
85         $dataset2data = array_merge($sharedtoprows, array(array('no', 'no', 'no')));
86         $f2 = $dataset2->store($dataset2data);
87         $dataset2->close_process();
89         $files = array($f1, $f2);
90         $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
91             \core_analytics\dataset_manager::LABELLED_FILEAREA);
93         $mergedfilecontents = $merged->get_content();
94         $this->assertContains('yeah', $mergedfilecontents);
95         $this->assertContains('no', $mergedfilecontents);
96         $this->assertContains('var1', $mergedfilecontents);
97         $this->assertContains('value1', $mergedfilecontents);
98         $this->assertContains('header1', $mergedfilecontents);
99     }