MDL-61568 repository_flickr: Fix oauth secret export
[moodle.git] / repository / flickr / tests / privacy_test.php
CommitLineData
202ec23a
ZT
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 * Privacy tests for repository_flickr.
18 *
19 * @package repository_flickr
20 * @category test
21 * @copyright 2018 Zig Tan <zig@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24defined('MOODLE_INTERNAL') || die();
25
26use \repository_flickr\privacy\provider;
27use \core_privacy\local\request\approved_contextlist;
28use \core_privacy\local\request\writer;
29use \core_privacy\tests\provider_testcase;
30
31/**
32 * Unit tests for repository/flickr/privacy/provider
33 *
34 * @copyright 2018 Zig Tan <zig@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class repository_flickr_privacy_testcase extends provider_testcase {
38 /**
39 * Overriding setUp() function to always reset after tests.
40 */
41 public function setUp() {
42 $this->resetAfterTest(true);
43 }
44
45 /**
46 * Test for provider::export_user_preferences().
47 */
48 public function test_export_user_preferences() {
49 global $DB;
50
51 // Test setup.
52 $user = $this->getDataGenerator()->create_user();
53 $this->setUser($user);
54 $contextlist = provider::get_contexts_for_userid($user->id);
55 $approvedcontextlist = new approved_contextlist($user, 'repository_flickr', $contextlist->get_contextids());
56 $user = $approvedcontextlist->get_user();
57 $contextuser = context_user::instance($user->id);
58
59 // Test exporting of Flickr repository user preferences *without* OAuth token/secret preference configured.
60 provider::export_user_preferences($user->id);
61 $writer = writer::with_context($contextuser);
62
63 // Verify there is no user preferences data exported.
64 $this->assertFalse($writer->has_any_data());
65
66 // Test exporting of Flickr repository user preferences *with* OAuth token/secret preference configured.
67 $preference = (object) [
68 'userid' => $user->id,
69 'name' => 'repository_flickr_access_token',
70 'value' => 'dummy flickr oauth access token'
71 ];
72 $DB->insert_record('user_preferences', $preference);
73 $preference = (object) [
74 'userid' => $user->id,
75 'name' => 'repository_flickr_access_token_secret',
76 'value' => 'dummy flickr oauth access token secret'
77 ];
78 $DB->insert_record('user_preferences', $preference);
79 provider::export_user_preferences($user->id);
80 $writer = writer::with_context($contextuser);
81
82 // Verify there is user preferences data exported.
83 $this->assertTrue($writer->has_any_data());
84 $userpreferences = $writer->get_user_preferences('repository_flickr');
85
86 // Verify the OAuth token is not an empty string value and the OAuth secret is an empty string value.
87 $accesstoken = $userpreferences->repository_flickr_access_token;
88 $this->assertFalse(empty($accesstoken->value));
89 $accesstokensecret = $userpreferences->repository_flickr_access_token_secret;
90 $this->assertTrue(empty($accesstokensecret->value));
91 }
92}