MDL-64049 core: Cache prefs for current user
[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.
8cba8d1a
AN
67 set_user_preferences([
68 'repository_flickr_access_token' => 'dummy flickr oauth access token',
69 'repository_flickr_access_token_secret' => 'dummy flickr oauth access token secret',
70 ], $user->id);
71
202ec23a
ZT
72 provider::export_user_preferences($user->id);
73 $writer = writer::with_context($contextuser);
74
75 // Verify there is user preferences data exported.
76 $this->assertTrue($writer->has_any_data());
77 $userpreferences = $writer->get_user_preferences('repository_flickr');
78
79 // Verify the OAuth token is not an empty string value and the OAuth secret is an empty string value.
80 $accesstoken = $userpreferences->repository_flickr_access_token;
81 $this->assertFalse(empty($accesstoken->value));
82 $accesstokensecret = $userpreferences->repository_flickr_access_token_secret;
83 $this->assertTrue(empty($accesstokensecret->value));
84 }
85}