| 1 | 
           efrain | 
           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/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           namespace core_customfield;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * core_customfield test data generator testcase.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package    core_customfield
  | 
        
        
            | 
            | 
           23 | 
            * @category   test
  | 
        
        
            | 
            | 
           24 | 
            * @copyright  2018 Ruslan Kabalin
  | 
        
        
            | 
            | 
           25 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           27 | 
           final class generator_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Get generator
  | 
        
        
            | 
            | 
           31 | 
                * @return core_customfield_generator
  | 
        
        
            | 
            | 
           32 | 
                */
  | 
        
        
            | 
            | 
           33 | 
               protected function get_generator(): \core_customfield_generator {
  | 
        
        
            | 
            | 
           34 | 
                   return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  | 
        
        
            | 
            | 
           35 | 
               }
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /**
  | 
        
        
            | 
            | 
           38 | 
                * Test creating category
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           40 | 
               public function test_create_category(): void {
  | 
        
        
           | 1 | 
           efrain | 
           41 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   $lpg = $this->get_generator();
  | 
        
        
            | 
            | 
           44 | 
                   $category = $lpg->create_category();
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
                   $this->assertInstanceOf('\core_customfield\category_controller', $category);
  | 
        
        
            | 
            | 
           47 | 
                   $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
  | 
        
        
            | 
            | 
           48 | 
               }
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
               /**
  | 
        
        
            | 
            | 
           51 | 
                * Test creating field
  | 
        
        
            | 
            | 
           52 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           53 | 
               public function test_create_field(): void {
  | 
        
        
           | 1 | 
           efrain | 
           54 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
                   $lpg = $this->get_generator();
  | 
        
        
            | 
            | 
           57 | 
                   $category = $lpg->create_category();
  | 
        
        
            | 
            | 
           58 | 
                   $field = $lpg->create_field(['categoryid' => $category->get('id')]);
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
                   $this->assertInstanceOf('\core_customfield\field_controller', $field);
  | 
        
        
            | 
            | 
           61 | 
                   $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $category = \core_customfield\category_controller::create($category->get('id'));
  | 
        
        
            | 
            | 
           64 | 
                   $category = \core_customfield\api::get_categories_with_fields($category->get('component'),
  | 
        
        
            | 
            | 
           65 | 
                       $category->get('area'), $category->get('itemid'))[$category->get('id')];
  | 
        
        
            | 
            | 
           66 | 
                   $this->assertCount(1, $category->get_fields());
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Test for function add_instance_data()
  | 
        
        
            | 
            | 
           71 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           72 | 
               public function test_add_instance_data(): void {
  | 
        
        
           | 1 | 
           efrain | 
           73 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
                   $lpg = $this->get_generator();
  | 
        
        
            | 
            | 
           76 | 
                   $c1 = $lpg->create_category();
  | 
        
        
            | 
            | 
           77 | 
                   $course1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
                   $f11 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'checkbox']);
  | 
        
        
            | 
            | 
           80 | 
                   $f12 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'date']);
  | 
        
        
            | 
            | 
           81 | 
                   $f13 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'),
  | 
        
        
            | 
            | 
           82 | 
                       'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
  | 
        
        
            | 
            | 
           83 | 
                   $f14 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'text']);
  | 
        
        
            | 
            | 
           84 | 
                   $f15 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'textarea']);
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
                   $this->get_generator()->add_instance_data($f11, $course1->id, 1);
  | 
        
        
            | 
            | 
           87 | 
                   $this->get_generator()->add_instance_data($f12, $course1->id, 1546300800);
  | 
        
        
            | 
            | 
           88 | 
                   $this->get_generator()->add_instance_data($f13, $course1->id, 2);
  | 
        
        
            | 
            | 
           89 | 
                   $this->get_generator()->add_instance_data($f14, $course1->id, 'Hello');
  | 
        
        
            | 
            | 
           90 | 
                   $this->get_generator()->add_instance_data($f15, $course1->id, ['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
                   $handler = $c1->get_handler();
  | 
        
        
            | 
            | 
           93 | 
                   list($data1, $data2, $data3, $data4, $data5) = array_values($handler->get_instance_data($course1->id));
  | 
        
        
            | 
            | 
           94 | 
                   $this->assertNotEmpty($data1->get('id'));
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertEquals(1, $data1->get_value());
  | 
        
        
            | 
            | 
           96 | 
                   $this->assertNotEmpty($data2->get('id'));
  | 
        
        
            | 
            | 
           97 | 
                   $this->assertEquals(1546300800, $data2->get_value());
  | 
        
        
            | 
            | 
           98 | 
                   $this->assertNotEmpty($data3->get('id'));
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertEquals(2, $data3->get_value());
  | 
        
        
            | 
            | 
           100 | 
                   $this->assertNotEmpty($data4->get('id'));
  | 
        
        
            | 
            | 
           101 | 
                   $this->assertEquals('Hello', $data4->get_value());
  | 
        
        
            | 
            | 
           102 | 
                   $this->assertNotEmpty($data5->get('id'));
  | 
        
        
            | 
            | 
           103 | 
                   $this->assertEquals('<p>Hi there</p>', $data5->get_value());
  | 
        
        
            | 
            | 
           104 | 
               }
  | 
        
        
            | 
            | 
           105 | 
           }
  |