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--.gitignore4
-rw-r--r--lib/controller/httpbindcontroller.php6
-rw-r--r--lib/db/iqroster.php7
-rw-r--r--lib/db/message.php4
-rw-r--r--lib/db/stanza.php8
-rw-r--r--phpunit.integration.xml2
-rw-r--r--phpunit.xml5
-rw-r--r--tests/bootstrap.php3
-rw-r--r--tests/integration/db/StanzaMapperTest.php58
-rw-r--r--tests/unit/controller/HttpBindControllerTest.php113
-rw-r--r--utility/mappertestutility.php46
11 files changed, 231 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index c66b26d..8dec5f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,7 +9,7 @@ node_modules/
archives/
css/
-# Composer
composer.phar
vendor/
-.phpstorm_helpers \ No newline at end of file
+.phpstorm_helpers
+coverage \ No newline at end of file
diff --git a/lib/controller/httpbindcontroller.php b/lib/controller/httpbindcontroller.php
index 778bcfa..f59fb46 100644
--- a/lib/controller/httpbindcontroller.php
+++ b/lib/controller/httpbindcontroller.php
@@ -159,16 +159,10 @@ class HttpBindController extends Controller {
switch($stanza['name']){
case '{jabber:client}message':
return self::MESSAGE;
- break;
case '{jabber:client}iq':
return self::IQ;
- break;
case '{jabber:client}presence':
return self::PRESENCE;
- break;
- default:
- return self::BODY;
- break;
}
}
} \ No newline at end of file
diff --git a/lib/db/iqroster.php b/lib/db/iqroster.php
index be7211d..9320c6a 100644
--- a/lib/db/iqroster.php
+++ b/lib/db/iqroster.php
@@ -2,7 +2,6 @@
namespace OCA\OJSXC\Db;
-use \OCP\AppFramework\Db\Entity;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use Sabre\Xml\XmlDeserializable;
@@ -10,9 +9,9 @@ use Sabre\Xml\XmlSerializable;
class IQRoster extends Stanza implements XmlSerializable{
- protected $type;
- protected $qid;
- protected $items;
+ public $type;
+ public $qid;
+ public $items;
public function xmlSerialize(Writer $writer) {
$writer->write([
diff --git a/lib/db/message.php b/lib/db/message.php
index 519ccc4..3fffcd6 100644
--- a/lib/db/message.php
+++ b/lib/db/message.php
@@ -10,8 +10,8 @@ use Sabre\Xml\XmlSerializable;
class Message extends Stanza implements XmlSerializable{
- protected $type;
- protected $values;
+ public $type;
+ public $values;
public function xmlSerialize(Writer $writer) {
$writer->write([
diff --git a/lib/db/stanza.php b/lib/db/stanza.php
index 8409edd..9915c57 100644
--- a/lib/db/stanza.php
+++ b/lib/db/stanza.php
@@ -16,11 +16,11 @@ use Sabre\Xml\XmlSerializable;
*/
class Stanza extends Entity implements XmlSerializable{
- protected $to;
- protected $from;
- protected $stanza;
+ public $to;
+ public $from;
+ public $stanza;
- function xmlSerialize(Writer $writer) {
+ public function xmlSerialize(Writer $writer) {
$writer->writeRaw($this->getStanza());
}
} \ No newline at end of file
diff --git a/phpunit.integration.xml b/phpunit.integration.xml
index c49bb10..ab9eb95 100644
--- a/phpunit.integration.xml
+++ b/phpunit.integration.xml
@@ -1,4 +1,4 @@
-<phpunit bootstrap="../../lib/base.php">
+<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="integration">
<directory>./tests/integration</directory>
diff --git a/phpunit.xml b/phpunit.xml
index 783af30..8cb6251 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -4,4 +4,9 @@
<directory>./tests/unit</directory>
</testsuite>
</testsuites>
+ <filter>
+ <whitelist processUncoveredFilesFromWhitelist="true">
+ <directory suffix=".php">lib</directory>
+ </whitelist>
+ </filter>
</phpunit> \ No newline at end of file
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..41d18f3
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,3 @@
+<?php
+require_once __DIR__ . '/../../../tests/bootstrap.php';
+require_once __DIR__ . '/../vendor/autoload.php'; \ No newline at end of file
diff --git a/tests/integration/db/StanzaMapperTest.php b/tests/integration/db/StanzaMapperTest.php
new file mode 100644
index 0000000..22ca394
--- /dev/null
+++ b/tests/integration/db/StanzaMapperTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace OCA\OJSXC\Db;
+
+use OCA\OJSXC\Utility\MapperTestUtility;
+
+/**
+ * @group DB
+ */
+class StanzaMapperTest extends MapperTestUtility {
+
+ /**
+ * @var StanzaMapper
+ */
+ protected $mapper;
+
+ protected function setUp() {
+ $this->entityName = 'OCA\OJSXC\Db\Stanza';
+ $this->mapperName = 'StanzaMapper';
+ parent::setUp();
+ }
+
+ public function insertProvider() {
+ return [
+ [
+ 'john@localhost',
+ 'thomas@localhost',
+ 'abcd'
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider insertProvider
+ */
+ public function testInsert($from, $to, $data) {
+ $stanza = new Stanza();
+ $stanza->setFrom($from);
+ $stanza->setTo($to);
+ $stanza->setStanza($data);
+
+ $this->assertEquals($stanza->getFrom(), $from);
+ $this->assertEquals($stanza->getTo(), $to);
+ $this->assertEquals($stanza->getStanza(), $data);
+
+ $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($stanza->getStanza(), $result[0]->getStanza());
+ }
+
+
+
+} \ No newline at end of file
diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php
index 9366da7..f6b3dc2 100644
--- a/tests/unit/controller/HttpBindControllerTest.php
+++ b/tests/unit/controller/HttpBindControllerTest.php
@@ -1,11 +1,14 @@
<?php
namespace OCA\OJSXC\Controller;
+use OCA\OJSXC\Db\Message;
+use OCA\OJSXC\Db\Stanza;
use OCA\OJSXC\Db\StanzaMapper;
use OCA\OJSXC\Http\XMPPResponse;
use OCA\OJSXC\StanzaHandlers\IQ;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit_Framework_TestCase;
+use Sabre\Xml\Writer;
class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
@@ -24,6 +27,11 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
*/
private $iqHandler;
+ /**
+ * @var PHPUnit_Framework_MockObject_MockObject
+ */
+ private $messageHandler;
+
private $userId = 'john';
public function setUp() {
@@ -40,7 +48,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->getMock();
$this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->getMock();
- $messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->getMock();
+ $this->messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->getMock();
$this->controller = new HttpBindController(
'ojsxc',
@@ -49,7 +57,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$session,
$this->stanzaMapper,
$this->iqHandler,
- $messageHandler,
+ $this->messageHandler,
'localhost',
$requestBody,
0,
@@ -61,7 +69,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
* When invalid XML, just start long polling.
*/
public function testInvalidXML() {
- $ex = new DoesNotExistException();
+ $ex = new DoesNotExistException('');
$expResponse = new XMPPResponse();
$this->setUpController('<x>');
@@ -78,12 +86,14 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
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>',
- '<body xmlns="http://jabber.org/protocol/httpbind"><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></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()
],
[
'<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)
]
];
@@ -92,12 +102,12 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider IQProvider
*/
- public function testIQHandler($body, $result, $pollCount) {
+ public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount) {
$ex = new DoesNotExistException();
$this->setUpController($body);
$expResponse = new XMPPResponse();
- $expResponse->write($result);
+ $expResponse->write($expected);
$this->iqHandler->expects($this->any()) // FIXME
->method('handle')
@@ -111,7 +121,98 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
$response = $this->controller->index();
$this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+
+ }
+
+ public function testDbResults() {
+ $result = 'test';
+ $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>');
+
+ $expResponse = new XMPPResponse();
+ $expResponse->write($result);
+
+ $this->iqHandler->expects($this->any()) // FIXME
+ ->method('handle')
+ ->will($this->returnValue($result));
+
+ $r1 = $this->getMockBuilder('Sabre\XML\XmlSerializable')->getMock();
+ $r1->expects($this->once())
+ ->method('xmlSerialize')
+ ->will($this->returnCallback(function(Writer $writer){
+ $writer->write('test');
+ }));
+
+ $this->stanzaMapper->expects($this->once())
+ ->method('findByTo')
+ ->with('john@localhost')
+ ->will($this->returnValue([$r1]));
+
+
+ $response = $this->controller->index();
+ $this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+ }
+
+ public function testMessageNoDbHandler() {
+ $body = '<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><request xmlns=\'urn:xmpp:receipts\'/></message></body>';
+ $ex = new DoesNotExistException();
+ $this->setUpController($body);
+
+ $expResponse = new XMPPResponse();
+
+ $this->messageHandler->expects($this->any()) // FIXME
+ ->method('handle');
+
+ $this->stanzaMapper->expects($this->exactly(10))
+ ->method('findByTo')
+ ->with('john@localhost')
+ ->will($this->throwException($ex));
+
+ $response = $this->controller->index();
+ $this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+ }
+
+ public function testMessageDbHandler() {
+ $body = '<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><request xmlns=\'urn:xmpp:receipts\'/></message></body>';
+ $this->setUpController($body);
+
+ $expResponse = new XMPPResponse();
+ $expResponse->write('test');
+
+ $this->messageHandler->expects($this->any()) // FIXME
+ ->method('handle');
+
+ $r1 = $this->getMockBuilder('Sabre\XML\XmlSerializable')->getMock();
+ $r1->expects($this->once())
+ ->method('xmlSerialize')
+ ->will($this->returnCallback(function(Writer $writer){
+ $writer->write('test');
+ }));
+
+ $this->stanzaMapper->expects($this->once())
+ ->method('findByTo')
+ ->with('john@localhost')
+ ->will($this->returnValue([$r1]));
+
+ $response = $this->controller->index();
+ $this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+ }
+
+ public function testPresenceHandler() {
+ $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><presence xmlns=\'jabber:client\'><c xmlns=\'http://jabber.org/protocol/caps\' hash=\'sha-1\' node=\'http://jsxc.org/\' ver=\'u2kAg/CbVmVZhsu+lZrkuLLdO+0=\'/><show>chat</show></presence></body>';
+ $this->setUpController($body);
+
+ $this->controller->index();
+ }
+
+ public function testBodyHandler() {
+ $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>';
+ $this->setUpController($body);
+ $this->controller->index();
}
} \ No newline at end of file
diff --git a/utility/mappertestutility.php b/utility/mappertestutility.php
new file mode 100644
index 0000000..0a705ce
--- /dev/null
+++ b/utility/mappertestutility.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace OCA\OJSXC\Utility;
+
+use Test\TestCase;
+use OCA\OJSXC\AppInfo\Application;
+
+
+/**
+ * @group DB
+ */
+class MapperTestUtility extends TestCase {
+
+ /**
+ * @var \OCP\AppFramework\IAppContainer
+ */
+ protected $container;
+
+ protected $entityName;
+
+ protected $mapperName;
+
+ protected function setUp() {
+ parent::setUp();
+ $app = new Application();
+ $this->container = $app->getContainer();
+ $this->mapper = $this->container[$this->mapperName];
+
+ $con = $this->container->getServer()->getDatabaseConnection();
+ $con->executeQuery('DELETE FROM ' . $this->mapper->getTableName());
+ }
+
+ protected function fetchAll(){
+ $con = $this->container->getServer()->getDatabaseConnection();
+ $stmt = $con->executeQuery('SELECT * FROM ' . $this->mapper->getTableName());
+ $entities = [];
+
+ while($row = $stmt->fetch()){
+ $entities[] = call_user_func($this->entityName . '::fromRow', $row);;
+ }
+
+ $stmt->closeCursor();
+
+ return $entities;
+ }
+} \ No newline at end of file