/** * Delete a template. */ public function test_delete_template() { global $DB; $syscontextid = context_system::instance()->id; $catcontextid = context_coursecat::instance($this->category->id)->id; // Creating a few templates. $this->setUser($this->creator); $sys1 = $this->create_template(1, true); $cat1 = $this->create_template(2, false); $cat2 = $this->create_template(3, false); $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $sys1->id))); $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat1->id))); $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat2->id))); // User without permissions. $this->setUser($this->user); try { external::delete_template($sys1->id); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { // All good. } try { external::delete_template($cat1->id); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { // All good. } // User with category permissions. $this->setUser($this->catcreator); try { external::delete_template($sys1->id); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { // All good. } $result = external::delete_template($cat1->id); $result = external_api::clean_returnvalue(external::delete_template_returns(), $result); $this->assertTrue($result); $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat1->id))); // User with system permissions. $this->setUser($this->creator); $result = external::delete_template($sys1->id); $result = external_api::clean_returnvalue(external::delete_template_returns(), $result); $this->assertTrue($result); $result = external::delete_template($cat2->id); $result = external_api::clean_returnvalue(external::delete_template_returns(), $result); $this->assertTrue($result); $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $sys1->id))); $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat2->id))); }