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

github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/controller/httpbindcontroller.php21
-rw-r--r--lib/db/stanza.php17
-rw-r--r--lib/stanzahandlers/iq.php6
-rw-r--r--lib/stanzahandlers/message.php3
-rw-r--r--tests/integration/MemLockTest.php2
-rw-r--r--tests/unit/controller/HttpBindControllerTest.php98
6 files changed, 121 insertions, 26 deletions
diff --git a/lib/controller/httpbindcontroller.php b/lib/controller/httpbindcontroller.php
index c70ea74..d6d8fc3 100644
--- a/lib/controller/httpbindcontroller.php
+++ b/lib/controller/httpbindcontroller.php
@@ -110,6 +110,7 @@ class HttpBindController extends Controller {
/**
* @NoAdminRequired
* @NoCSRFRequired
+ * @return XMPPResponse
*/
public function index() {
$this->lock->setLock();
@@ -129,15 +130,17 @@ class HttpBindController extends Controller {
} catch (LibXMLException $e){
}
$stanzas = $stanzas['value'];
- foreach($stanzas as $stanza) {
- $stanzaType = $this->getStanzaType($stanza);
- if ($stanzaType === self::MESSAGE) {
- $this->messageHandler->handle($stanza);
- } else if ($stanzaType === self::IQ){
- $result = $this->iqHandler->handle($stanza);
- if (!is_null($result)) {
- $longpoll = false;
- $this->response->write($result);
+ if (is_array($stanzas)) {
+ foreach ($stanzas as $stanza) {
+ $stanzaType = $this->getStanzaType($stanza);
+ if ($stanzaType === self::MESSAGE) {
+ $this->messageHandler->handle($stanza);
+ } else if ($stanzaType === self::IQ) {
+ $result = $this->iqHandler->handle($stanza);
+ if (!is_null($result)) {
+ $longpoll = false;
+ $this->response->write($result);
+ }
}
}
}
diff --git a/lib/db/stanza.php b/lib/db/stanza.php
index 9915c57..bd7722f 100644
--- a/lib/db/stanza.php
+++ b/lib/db/stanza.php
@@ -13,11 +13,28 @@ use Sabre\Xml\XmlSerializable;
* @package OCA\OJSXC\Db
* @brief this class is used as the entity which is fetched from the stanza table OR extended by a specific stanza
* for inserting into the stanza table
+ * @method string getTo()
+ * @method string getFrom()
+ * @method string getStanza()
+ * @method void setTo(string $to)
+ * @method void setFrom(string $from)
+ * @method void setStanza(string $stanza)
*/
class Stanza extends Entity implements XmlSerializable{
+ /**
+ * @var string $to
+ */
public $to;
+
+ /**
+ * @var string $to
+ */
public $from;
+
+ /**
+ * @var string $to
+ */
public $stanza;
public function xmlSerialize(Writer $writer) {
diff --git a/lib/stanzahandlers/iq.php b/lib/stanzahandlers/iq.php
index baf3baa..025f676 100644
--- a/lib/stanzahandlers/iq.php
+++ b/lib/stanzahandlers/iq.php
@@ -23,10 +23,14 @@ class IQ extends StanzaHandler {
}
+ /**
+ * @param $stanza
+ * @return IQRoster
+ */
public function handle($stanza) {
$this->to = $this->getAttribute($stanza, 'to');
- foreach($stanza['value'] as $value){
+ foreach($stanza['value'] as $value){ // TODO
if ($value['name'] === '{jabber:iq:roster}query'){
$id = $stanza['attributes']['id'];
$iqRoster = new IQRoster();
diff --git a/lib/stanzahandlers/message.php b/lib/stanzahandlers/message.php
index e8a354a..c639eca 100644
--- a/lib/stanzahandlers/message.php
+++ b/lib/stanzahandlers/message.php
@@ -5,6 +5,7 @@ namespace OCA\OJSXC\StanzaHandlers;
use OCA\OJSXC\Db\MessageMapper;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
+use OCA\OJSXC\Db\Stanza;
class Message extends StanzaHandler {
@@ -21,7 +22,7 @@ class Message extends StanzaHandler {
$this->messageMapper = $messageMapper;
}
- public function handle($stanza) {
+ public function handle(array $stanza) {
$to = $this->getAttribute($stanza, 'to');
$pos = strpos($to, '@');
$this->to = substr($to, 0, $pos);
diff --git a/tests/integration/MemLockTest.php b/tests/integration/MemLockTest.php
index 2a97e1b..e1d04f1 100644
--- a/tests/integration/MemLockTest.php
+++ b/tests/integration/MemLockTest.php
@@ -54,7 +54,7 @@ class MemLockTest extends TestCase {
if ($cache->isAvailable()) {
$this->memCache = $cache->create('ojsxc');
} else {
- die('No memcache available'); // TODO
+ die('No memcache available');
}
$this->memLock = new MemLock(
diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php
index d51d294..36a8a35 100644
--- a/tests/unit/controller/HttpBindControllerTest.php
+++ b/tests/unit/controller/HttpBindControllerTest.php
@@ -102,18 +102,30 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
}
public function IQProvider() {
+ $expStanza1 = new Stanza();
+ $expStanza1->setStanza('<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>');
+
+ $result1 = new Stanza();
+ $result1->setStanza('<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>');
+
+ $result2 = new Stanza();
+ $result2->setStanza(null);
+ $expStanza2 = new Stanza();
+ $expStanza2->setStanza(null);
return [
[
'<body rid=\'897878733\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>',
- '<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>',
- '<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>', // we ask for 3 IQ's thus return 3 values
- $this->once()
+ $result1,
+ $expStanza1, // we ask for 3 IQ's thus return 3 values
+ $this->once(),
+ $this->exactly(3)
],
[
'<body rid=\'897878734\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>',
- null,
- null,
- $this->exactly(10)
+ $result2,
+ $expStanza2,
+ $this->once(),
+ $this->exactly(3)
]
];
}
@@ -121,14 +133,14 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider IQProvider
*/
- public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount) {
+ public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount, $handlerCount) {
$ex = new DoesNotExistException();
$this->setUpController($body);
$this->mockLock();
$expResponse = new XMPPResponse();
$expResponse->write($expected);
- $this->iqHandler->expects($this->any()) // FIXME
+ $this->iqHandler->expects($handlerCount)
->method('handle')
->will($this->returnValue($result));
@@ -152,7 +164,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$expResponse = new XMPPResponse();
$expResponse->write($result);
- $this->iqHandler->expects($this->any()) // FIXME
+ $this->iqHandler->expects($this->never())
->method('handle')
->will($this->returnValue($result));
@@ -182,9 +194,69 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$expResponse = new XMPPResponse();
- $this->messageHandler->expects($this->any()) // FIXME
+ $this->messageHandler->expects($this->once())
+ ->method('handle');
+
+ $this->stanzaMapper->expects($this->exactly(10))
+ ->method('findByTo')
+ ->with('john')
+ ->will($this->throwException($ex));
+
+ $response = $this->controller->index();
+ $this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+ }
+
+
+ public function testMultipleMessageNoDbHandler() {
+ echo "================================================";
+ $body = <<<XML
+ <body rid='897878959' xmlns='http://jabber.org/protocol/httpbind' sid='7862'>
+ <message to='derp@own.dev' type='chat' id='1452960296859-msg' xmlns='jabber:client'><body>abc</body></message>
+ <message to='derp@own.dev' type='chat' id='1452960296860-msg' xmlns='jabber:client'><body>abc2</body></message>
+ <message to='derp@own.dev' type='chat' id='1452960296861-msg' xmlns='jabber:client'><body>abc3</body></message>
+ </body>
+XML;
+ $ex = new DoesNotExistException();
+ $this->setUpController($body);
+ $this->mockLock();
+
+ $expResponse = new XMPPResponse();
+ $this->messageHandler->expects($this->any())
->method('handle');
+ $this->messageHandler->expects($this->exactly(3))
+ ->method('handle')
+ ->withConsecutive(
+ $this->equalTo(
+ [ 'name' => '{jabber:client}message',
+ ' value' => [
+ '{jabber:client}body' => 'abc',
+ ],
+ 'attributes' => [
+ 'to' => 'derp@own.dev',
+ 'type' => 'chat',
+ 'id' => '1452960296859-msg',
+ ]]),
+ $this->equalTo([ 'name' => '{jabber:client}message',
+ 'value' => [
+ '{jabber:client}body' => 'abc2',
+ ], 'attributes' => [
+ 'to' => 'derp@own.dev',
+ 'type' => 'chat',
+ 'id' => '1452960296860-msg',
+ ]]),
+ $this->equalTo([ 'name' => '{jabber:client}message',
+ 'value' => [
+ '{jabber:client}body' => 'abc3',
+ ], 'attributes' => [
+ 'to' => 'derp@own.dev',
+ 'type' => 'chat',
+ 'id' => '1452960296861-msg',
+ ]])
+
+ );
+
$this->stanzaMapper->expects($this->exactly(10))
->method('findByTo')
->with('john')
@@ -203,8 +275,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$expResponse = new XMPPResponse();
$expResponse->write('test');
- $this->messageHandler->expects($this->any()) // FIXME
- ->method('handle');
+ $this->messageHandler->expects($this->once())
+ ->method('handle');
$r1 = $this->getMockBuilder('Sabre\XML\XmlSerializable')->disableOriginalConstructor()->getMock();
$r1->expects($this->once())
@@ -239,6 +311,4 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$this->controller->index();
}
-
-
}