Merge branch 'MDL-62285-master' of git://github.com/ryanwyllie/moodle
authorAndrew Nicols <andrew@nicols.co.uk>
Tue, 8 May 2018 03:08:03 +0000 (11:08 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 8 May 2018 03:08:03 +0000 (11:08 +0800)
1  2 
privacy/classes/local/request/moodle_content_writer.php
privacy/tests/moodle_content_writer_test.php

@@@ -244,7 -244,9 +244,9 @@@ class moodle_content_writer implements 
          $path = [];
          $contexts = array_reverse($this->context->get_parent_contexts(true));
          foreach ($contexts as $context) {
-             $path[] = shorten_filename(clean_param($context->get_context_name(), PARAM_FILE), MAX_FILENAME_SIZE, true);
+             $name = $context->get_context_name();
+             $id = $context->id;
 -            $path[] = clean_param("{$name} {$id}", PARAM_FILE);
++            $path[] = shorten_filename(clean_param("{$name} {$id}", PARAM_FILE), MAX_FILENAME_SIZE, true);
          }
  
          return $path;
@@@ -913,151 -969,6 +969,151 @@@ class moodle_content_writer_test extend
          ];
      }
  
-         $expectedpath = 'System/'.$expected.'/data.json';
 +    /**
 +     * Test that exported data is shortened when exceeds the limit.
 +     *
 +     * @dataProvider long_filename_provider
 +     * @param string $longtext
 +     * @param string $expected
 +     * @param string $text
 +     */
 +    public function test_export_data_long_filename($longtext, $expected, $text) {
 +        $context = \context_system::instance();
 +        $subcontext = [$longtext];
 +        $data = (object) ['key' => $text];
 +
 +        $writer = $this->get_writer_instance()
 +                ->set_context($context)
 +                ->export_data($subcontext, $data);
 +
 +        $fileroot = $this->fetch_exported_content($writer);
 +
 +        $contextpath = $this->get_context_path($context, $subcontext, 'data.json');
-         $expectedpath = 'System/'.$expected.'/name.json';
++        $expectedpath = "System {$context->id}/{$expected}/data.json";
 +        $this->assertEquals($expectedpath, $contextpath);
 +
 +        $json = $fileroot->getChild($contextpath)->getContent();
 +        $this->assertRegExp("/$text/", $json);
 +
 +        $expanded = json_decode($json);
 +        $this->assertEquals($data, $expanded);
 +    }
 +
 +    /**
 +     * Test that exported related data is shortened when exceeds the limit.
 +     *
 +     * @dataProvider long_filename_provider
 +     * @param string $longtext
 +     * @param string $expected
 +     * @param string $text
 +     */
 +    public function test_export_related_data_long_filename($longtext, $expected, $text) {
 +        $context = \context_system::instance();
 +        $subcontext = [$longtext];
 +        $data = (object) ['key' => $text];
 +
 +        $writer = $this->get_writer_instance()
 +                ->set_context($context)
 +                ->export_related_data($subcontext, 'name', $data);
 +
 +        $fileroot = $this->fetch_exported_content($writer);
 +
 +        $contextpath = $this->get_context_path($context, $subcontext, 'name.json');
-         $expectedpath = 'System/'.$expected.'/metadata.json';
++        $expectedpath = "System {$context->id}/{$expected}/name.json";
 +        $this->assertEquals($expectedpath, $contextpath);
 +
 +        $json = $fileroot->getChild($contextpath)->getContent();
 +        $this->assertRegExp("/$text/", $json);
 +
 +        $expanded = json_decode($json);
 +        $this->assertEquals($data, $expanded);
 +    }
 +
 +    /**
 +     * Test that exported metadata is shortened when exceeds the limit.
 +     *
 +     * @dataProvider long_filename_provider
 +     * @param string $longtext
 +     * @param string $expected
 +     * @param string $text
 +     */
 +    public function test_export_metadata_long_filename($longtext, $expected, $text) {
 +        $context = \context_system::instance();
 +        $subcontext = [$longtext];
 +        $data = (object) ['key' => $text];
 +
 +        $writer = $this->get_writer_instance()
 +                ->set_context($context)
 +                ->export_metadata($subcontext, $text, $text, $text);
 +
 +        $fileroot = $this->fetch_exported_content($writer);
 +
 +        $contextpath = $this->get_context_path($context, $subcontext, 'metadata.json');
-         $expectedpath = 'System/User preferences/'.$expected.'.json';
++        $expectedpath = "System {$context->id}/{$expected}/metadata.json";
 +        $this->assertEquals($expectedpath, $contextpath);
 +
 +        $json = $fileroot->getChild($contextpath)->getContent();
 +        $this->assertRegExp("/$text.*$text.*$text/", $json);
 +
 +        $expanded = json_decode($json);
 +        $this->assertTrue(isset($expanded->$text));
 +        $this->assertEquals($text, $expanded->$text->value);
 +        $this->assertEquals($text, $expanded->$text->description);
 +    }
 +
 +    /**
 +     * Test that exported user preference is shortened when exceeds the limit.
 +     *
 +     * @dataProvider long_filename_provider
 +     * @param string $longtext
 +     * @param string $expected
 +     * @param string $text
 +     */
 +    public function test_export_user_preference_long_filename($longtext, $expected, $text) {
 +        $this->resetAfterTest();
 +
 +        if (!array_key_exists('json', core_filetypes::get_types())) {
 +            // Add json as mime type to avoid lose the extension when shortening filenames.
 +            core_filetypes::add_type('json', 'application/json', 'archive', [], '', 'JSON file archive');
 +        }
 +        $context = \context_system::instance();
++        $expectedpath = "System {$context->id}/User preferences/{$expected}.json";
++
 +        $component = $longtext;
 +
 +        $writer = $this->get_writer_instance()
 +                ->set_context($context)
 +                ->export_user_preference($component, $text, $text, $text);
 +
 +        $fileroot = $this->fetch_exported_content($writer);
 +
 +        $contextpath = $this->get_context_path($context, [get_string('userpreferences')], "{$component}.json");
 +        $this->assertEquals($expectedpath, $contextpath);
 +
 +        $json = $fileroot->getChild($contextpath)->getContent();
 +        $this->assertRegExp("/$text.*$text.*$text/", $json);
 +
 +        $expanded = json_decode($json);
 +        $this->assertTrue(isset($expanded->$text));
 +        $this->assertEquals($text, $expanded->$text->value);
 +        $this->assertEquals($text, $expanded->$text->description);
 +    }
 +
 +    /**
 +     * Provider for long filenames.
 +     *
 +     * @return array
 +     */
 +    public function long_filename_provider() {
 +        return [
 +            'More than 100 characters' => [
 +                'Etiam sit amet dui vel leo blandit viverra. Proin viverra suscipit velit. Aenean efficitur suscipit nibh nec suscipit',
 +                'Etiam sit amet dui vel leo blandit viverra. Proin viverra suscipit velit. Aenean effici - 22f7a5030d',
 +                'value',
 +            ],
 +        ];
 +    }
 +
      /**
       * Get a fresh content writer.
       *