summaryrefslogtreecommitdiffstats
path: root/tests/unit/db/postgres/ItemMapperTest.php
blob: 8677d8784079dc915c084c6cbabee41ce0522f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Alessandro Cosentino 2012
 * @copyright Bernhard Posselt 2012, 2014
 */

namespace OCA\News\Db\Postgres;

use \OCA\News\Db\Item;
use \OCA\News\Db\StatusFlag;


require_once(__DIR__ . "/../../../classloader.php");


class ItemMapperTest extends \OCA\News\Utility\MapperTestUtility {

	private $mapper;
	private $items;
	private $newestItemId;
	private $limit;
	private $user;
	private $offset;
	private $updatedSince;
	private $status;


	public function setUp()
	{
		$this->beforeEach();

		$this->mapper = new ItemMapper($this->db);

		// create mock items
		$item1 = new Item();
		$item2 = new Item();

		$this->items = array(
			$item1,
			$item2
		);

		$this->userId = 'john';
		$this->id = 3;
		$this->folderId = 2;

		$this->row = array(
			array('id' => $this->items[0]->getId()),
		);

		$this->rows = array(
			array('id' => $this->items[0]->getId()),
			array('id' => $this->items[1]->getId())
		);

		$this->user = 'john';
		$this->limit = 10;
		$this->offset = 3;
		$this->id = 11;
		$this->status = 333;
		$this->updatedSince = 323;
		$this->newestItemId = 2;

	}


	private function makeSelectQuery($prependTo){
		return 'SELECT `items`.* FROM `*PREFIX*news_items` `items` '.
			'JOIN `*PREFIX*news_feeds` `feeds` ' .
				'ON `feeds`.`id` = `items`.`feed_id` '.
				'AND `feeds`.`deleted_at` = 0 ' .
				'AND `feeds`.`user_id` = ? ' .
				$prependTo .
			'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' .
				'ON `folders`.`id` = `feeds`.`folder_id` ' .
			'WHERE `feeds`.`folder_id` = 0 ' .
				'OR `folders`.`deleted_at` = 0 ' .
			'ORDER BY `items`.`id` DESC';
	}

	private function makeSelectQueryStatus($prependTo, $status) {
		$status = (int) $status;

		return $this->makeSelectQuery(
			'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') ' .
			$prependTo
		);
	}


	public function testDeleteReadOlderThanThresholdDoesNotDeleteBelowThreshold(){
		$status = StatusFlag::STARRED | StatusFlag::UNREAD;
		$sql = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' .
		'`items`.`feed_id` AS `feed_id` ' . 
			'FROM `*PREFIX*news_items` `items` ' .
			'JOIN `*PREFIX*news_feeds` `feeds` ' .
				'ON `feeds`.`id` = `items`.`feed_id` ' .
			'WHERE NOT ((`items`.`status` & ?) > 0) ' .
			'GROUP BY `items`.`feed_id`, `feeds`.`articles_per_update` ' .
			'HAVING COUNT(*) > ?';

		$threshold = 10;
		$rows = array(array('feed_id' => 30, 'size' => 9));
		$params = array($status, $threshold);

		$this->setMapperResult($sql, $params, $rows);
		$this->mapper->deleteReadOlderThanThreshold($threshold);


	}


	public function testDeleteReadOlderThanThreshold(){
		$threshold = 10;
		$status = StatusFlag::STARRED | StatusFlag::UNREAD;

		$sql1 = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' .
		'`items`.`feed_id` AS `feed_id` ' . 
			'FROM `*PREFIX*news_items` `items` ' .
			'JOIN `*PREFIX*news_feeds` `feeds` ' .
				'ON `feeds`.`id` = `items`.`feed_id` ' .
			'WHERE NOT ((`items`.`status` & ?) > 0) ' .
			'GROUP BY `items`.`feed_id`, `feeds`.`articles_per_update` ' .
			'HAVING COUNT(*) > ?';
		$params1 = array($status, $threshold);


		$row = array('feed_id' => 30, 'size' => 11);

		$sql2 = 'DELETE FROM `*PREFIX*news_items` ' .
				'WHERE `id` IN (' .
					'SELECT `id` FROM `*PREFIX*news_items` ' .
					'WHERE NOT ((`status` & ?) > 0) ' .
					'AND `feed_id` = ? ' .
					'ORDER BY `id` ASC ' .
					'LIMIT ?' .
				')';
		$params2 = array($status, 30, 1);


		$this->setMapperResult($sql1, $params1, array($row));
		$this->setMapperResult($sql2, $params2);

		$this->mapper->deleteReadOlderThanThreshold($threshold);
	}


}