<commit_before><?php
/**
* ownCloud
*
* @author <NAME>
* @copyright 2012 <NAME> <EMAIL>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

class Test_Share extends \Test\TestCase {

	protected $itemType;
	protected $userBackend;
	protected $user1;
	protected $user2;
	protected $user3;
	protected $user4;
	protected $user5;
	protected $user6;
	protected $groupAndUser;
	protected $groupBackend;
	protected $group1;
	protected $group2;
	protected $resharing;
	protected $dateInFuture;
	protected $dateInPast;

	protected function setUp() {
		parent::setUp();

		OC_User::clearBackends();
		OC_User::useBackend('dummy');
		$this->user1 = $this->getUniqueID('user1_');
		$this->user2 = $this->getUniqueID('user2_');
		$this->user3 = $this->getUniqueID('user3_');
		$this->user4 = $this->getUniqueID('user4_');
		$this->user5 = $this->getUniqueID('user5_');
		$this->user6 = $this->getUniqueID('user6_');
		$this->groupAndUser = $this->getUniqueID('groupAndUser_');
		OC_User::createUser($this->user1, 'pass');
		OC_User::createUser($this->user2, 'pass');
		OC_User::createUser($this->user3, 'pass');
		OC_User::createUser($this->user4, 'pass');
		OC_User::createUser($this->user5, 'pass');
		OC_User::createUser($this->user6, 'pass'); // no group
		OC_User::createUser($this->groupAndUser, 'pass');
		OC_User::setUserId($this->user1);
		OC_Group::clearBackends();
		OC_Group::useBackend(new OC_Group_Dummy);
		$this->group1 = $this->getUniqueID('group1_');
		$this->group2 = $this->getUniqueID('group2_');
		OC_Group::createGroup($this->group1);
		OC_Group::createGroup($this->group2);
		OC_Group::createGroup($this->groupAndUser);
		OC_Group::addToGroup($this->user1, $this->group1);
		OC_Group::addToGroup($this->user2, $this->group1);
		OC_Group::addToGroup($this->user3, $this->group1);
		OC_Group::addToGroup($this->user2, $this->group2);
		OC_Group::addToGroup($this->user4, $this->group2);
		OC_Group::addToGroup($this->user2, $this->groupAndUser);
		OC_Group::addToGroup($this->user3, $this->groupAndUser);
		OCP\Share::registerBackend('test', 'Test_Share_Backend');
		OC_Hook::clear('OCP\\Share');
		OC::registerShareHooks();
		$this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');

		// 20 Minutes in the past, 20 minutes in the future.
		$now = time();
		$dateFormat = 'Y-m-d H:i:s';
		$this->dateInPast = date($dateFormat, $now - 20 * 60);
		$this->dateInFuture = date($dateFormat, $now + 20 * 60);
	}

	protected function tearDown() {
		$query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
		$query->execute(array('test'));
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);

		OC_User::deleteUser($this->user1);
		OC_User::deleteUser($this->user2);
		OC_User::deleteUser($this->user3);
		OC_User::deleteUser($this->user4);
		OC_User::deleteUser($this->user5);
		OC_User::deleteUser($this->user6);
		OC_User::deleteUser($this->groupAndUser);

		OC_Group::deleteGroup($this->group1);
		OC_Group::deleteGroup($this->group2);
		OC_Group::deleteGroup($this->groupAndUser);

		parent::tearDown();
	}

	public function testShareInvalidShareType() {
		$message = 'Share type foobar is not valid for test.txt';
		try {
			OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}
	}

	public function testInvalidItemType() {
		$message = 'Sharing backend for foobar not found';
		try {
			OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}
		try {
			OCP\Share::getItemsSharedWith('foobar');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}<commit_msg>Add logout in share test to avoid nasty side effects<commit_after><?php
/**
* ownCloud
*
* @author <NAME>
* @copyright 2012 <NAME> <EMAIL>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

class Test_Share extends \Test\TestCase {

	protected $itemType;
	protected $userBackend;
	protected $user1;
	protected $user2;
	protected $user3;
	protected $user4;
	protected $user5;
	protected $user6;
	protected $groupAndUser;
	protected $groupBackend;
	protected $group1;
	protected $group2;
	protected $resharing;
	protected $dateInFuture;
	protected $dateInPast;

	protected function setUp() {
		parent::setUp();

		OC_User::clearBackends();
		OC_User::useBackend('dummy');
		$this->user1 = $this->getUniqueID('user1_');
		$this->user2 = $this->getUniqueID('user2_');
		$this->user3 = $this->getUniqueID('user3_');
		$this->user4 = $this->getUniqueID('user4_');
		$this->user5 = $this->getUniqueID('user5_');
		$this->user6 = $this->getUniqueID('user6_');
		$this->groupAndUser = $this->getUniqueID('groupAndUser_');
		OC_User::createUser($this->user1, 'pass');
		OC_User::createUser($this->user2, 'pass');
		OC_User::createUser($this->user3, 'pass');
		OC_User::createUser($this->user4, 'pass');
		OC_User::createUser($this->user5, 'pass');
		OC_User::createUser($this->user6, 'pass'); // no group
		OC_User::createUser($this->groupAndUser, 'pass');
		OC_User::setUserId($this->user1);
		OC_Group::clearBackends();
		OC_Group::useBackend(new OC_Group_Dummy);
		$this->group1 = $this->getUniqueID('group1_');
		$this->group2 = $this->getUniqueID('group2_');
		OC_Group::createGroup($this->group1);
		OC_Group::createGroup($this->group2);
		OC_Group::createGroup($this->groupAndUser);
		OC_Group::addToGroup($this->user1, $this->group1);
		OC_Group::addToGroup($this->user2, $this->group1);
		OC_Group::addToGroup($this->user3, $this->group1);
		OC_Group::addToGroup($this->user2, $this->group2);
		OC_Group::addToGroup($this->user4, $this->group2);
		OC_Group::addToGroup($this->user2, $this->groupAndUser);
		OC_Group::addToGroup($this->user3, $this->groupAndUser);
		OCP\Share::registerBackend('test', 'Test_Share_Backend');
		OC_Hook::clear('OCP\\Share');
		OC::registerShareHooks();
		$this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');

		// 20 Minutes in the past, 20 minutes in the future.
		$now = time();
		$dateFormat = 'Y-m-d H:i:s';
		$this->dateInPast = date($dateFormat, $now - 20 * 60);
		$this->dateInFuture = date($dateFormat, $now + 20 * 60);
	}

	protected function tearDown() {
		$query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
		$query->execute(array('test'));
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);

		OC_User::deleteUser($this->user1);
		OC_User::deleteUser($this->user2);
		OC_User::deleteUser($this->user3);
		OC_User::deleteUser($this->user4);
		OC_User::deleteUser($this->user5);
		OC_User::deleteUser($this->user6);
		OC_User::deleteUser($this->groupAndUser);

		OC_Group::deleteGroup($this->group1);
		OC_Group::deleteGroup($this->group2);
		OC_Group::deleteGroup($this->groupAndUser);

		$this->logout();
		parent::tearDown();
	}

	public function testShareInvalidShareType() {
		$message = 'Share type foobar is not valid for test.txt';
		try {
			OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}
	}

	public function testInvalidItemType() {
		$message = 'Sharing backend for foobar not found';
		try {
			OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}
		try {
			OCP\Share::getItemsSharedWith('foobar');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
			$this->assertEquals($message, $exception->getMessage());
		}
{'fraction_non_alphanumeric': 0.1722671203743088, 'fraction_numerical': 0.015312632922160783, 'mean_word_length': 5.785714285714286, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 148, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7189299', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3175, 'n_words': 654}