Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MessageMapperTest.php « db « integration « tests - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7094b94285ed10a30fa1ed5cbf8d751739d06bc7 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

namespace OCA\OJSXC\Db;

use OCA\OJSXC\AppInfo\Application;
use OCA\OJSXC\Utility\MapperTestUtility;
use OCP\AppFramework\Db\DoesNotExistException;

function uniqid()
{
	return 4; // chosen by fair dice roll.
			  // guaranteed to be unique.
}

/**
 * @group DB
 */
class MessageMapperTest extends MapperTestUtility
{

	/**
	 * @var StanzaMapper
	 */
	protected $mapper;

	protected function setUp()
	{
		$this->entityName = 'OCA\OJSXC\Db\Message';
		$this->mapperName = 'MessageMapper';
		parent::setUp();
	}

	public function insertProvider()
	{
		return [
			[
				['john', 'localhost'],
				['thomas', 'localhost'],
				'abcd',
				'test',
				'Test Message',
				// save stanza without host or resource
				'<message to="thomas" from="john" type="test" xmlns="jabber:client" id="4-msg">Test Message</message>'
			]
		];
	}

	/**
	 * @dataProvider insertProvider
	 */
	public function testInsert($from, $to, $data, $type, $msg, $expectedStanza)
	{
		$stanza = new Message();
		$stanza->setFrom($from[0]);
		$stanza->setTo($to[0]);
		$stanza->setStanza($data);
		$stanza->setType($type);
		$stanza->setValue($msg);

		$this->assertEquals($stanza->getFrom(), $from[0]);
		$this->assertEquals($stanza->getTo(), $to[0]);
		$this->assertEquals($stanza->getStanza(), $data);
		$this->assertEquals($stanza->getType(), $type);

		$this->mapper->insert($stanza);

		$result = $this->fetchAll();

		$this->assertCount(1, $result);
		$this->assertEquals($stanza->getFrom(), $result[0]->getFrom());
		$this->assertEquals($stanza->getTo(), $result[0]->getTo());
		$this->assertEquals($expectedStanza, $result[0]->getStanza());
		$this->assertEquals(null, $result[0]->getType()); // type is saved into the XML string, not the DB.
	}

	/**
	 * @expectedException \OCP\AppFramework\Db\DoesNotExistException
	 */
	public function testFindByToNotFound()
	{
		$this->mapper->findByTo('test');
	}

	/**
	 * @expectedException \OCP\AppFramework\Db\DoesNotExistException
	 */
	public function testFindByToNotFound2()
	{
		$stanza = new Message();
		$stanza->setFrom('john', 'localhost');
		$stanza->setTo('john', 'localhost');
		$stanza->setStanza('abcd');
		$stanza->setType('test');
		$stanza->setValue('message abc');
		$this->mapper->insert($stanza);

		$this->mapper->findByTo('test');
	}

	public function testFindByToFoundWithoutAtSign()
	{
		// when the username doesn't contain a @ the domain is removed and stored as such in the DB
		// the resulting stanza then contains the full JID
		$stanza1 = new Message();
		$stanza1->setFrom('jan');
		$stanza1->setTo('john');
		$stanza1->setType('test');
		$stanza1->setValue('Messageabc');
		$this->mapper->insert($stanza1);

		$stanza2 = new Message();
		$stanza2->setFrom('thomas');
		$stanza2->setTo('jan');
		$stanza2->setType('test2');
		$stanza2->setValue('Message');
		$this->mapper->insert($stanza2);


		// check if two elements are inserted
		$result = $this->fetchAll();
		$this->assertCount(2, $result);

		// check findByTo
		$result = $this->mapper->findByTo('john');
		$this->assertCount(1, $result);
		$this->assertEquals('<message to="john@localhost/internal" from="jan@localhost/internal" type="test" xmlns="jabber:client" id="4-msg">Messageabc</message>', $result[0]->getStanza());

		// check if element is deleted
		$result = $this->fetchAll();
		$this->assertCount(1, $result);
		$this->assertEquals($stanza2->getFrom(), $result[0]->getFrom());
		$this->assertEquals($stanza2->getTo(), $result[0]->getTo());
		$this->assertEquals('<message to="jan" from="thomas" type="test2" xmlns="jabber:client" id="4-msg">Message</message>', $result[0]->getStanza()); // notice that the username isn't replaced by the JID since this tis the task of hte findByTo method
	}

	public function testFindByToFoundWithAtSign()
	{
		// when the username does contain a @ the domain is removed and stored as such in the DB, but with the @ still
		// in the username, the resulting stanza then contains the full JID
		$stanza1 = new Message();
		$stanza1->setFrom('jan@localhost.com');
		$stanza1->setTo('john@localhost.com');
		$stanza1->setStanza('abcd1');
		$stanza1->setType('test');
		$stanza1->setValue('Messageabc');
		$this->mapper->insert($stanza1);

		$stanza2 = new Message();
		$stanza2->setFrom('thomas@localhost.com');
		$stanza2->setTo('jan@localhost.com');
		$stanza2->setStanza('abcd2');
		$stanza2->setType('test2');
		$stanza2->setValue('Message');
		$this->mapper->insert($stanza2);


		// check if two elements are inserted
		$result = $this->fetchAll();
		$this->assertCount(2, $result);

		// check findByTo
		$result = $this->mapper->findByTo(Application::santizeUserId('john@localhost.com'));
		$this->assertCount(1, $result);
		$this->assertEquals('<message to="john_ojsxc_esc_at_localhost.com@localhost/internal" from="jan_ojsxc_esc_at_localhost.com@localhost/internal" type="test" xmlns="jabber:client" id="4-msg">Messageabc</message>', $result[0]->getStanza());

		// check if element is deleted
		$result = $this->fetchAll();
		$this->assertCount(1, $result);
		$this->assertEquals($stanza2->getFrom(), $result[0]->getFrom());
		$this->assertEquals($stanza2->getTo(), $result[0]->getTo());
		$this->assertEquals('<message to="jan_ojsxc_esc_at_localhost.com" from="thomas_ojsxc_esc_at_localhost.com" type="test2" xmlns="jabber:client" id="4-msg">Message</message>', $result[0]->getStanza());
	}
}