This commit is contained in:
LEDfan 2016-01-15 17:11:21 +01:00
Родитель b292c05788
Коммит bb858d48f1
11 изменённых файлов: 1142 добавлений и 20 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -12,3 +12,4 @@ css/
# Composer
composer.phar
vendor/
.phpstorm_helpers

Просмотреть файл

@ -24,7 +24,10 @@ class Application extends App {
$c->query('StanzaMapper'),
$c->query('IQHandler'),
$c->query('MessageHandler'),
$c->query('Host')
$c->query('Host'),
file_get_contents("php://input"),
1, // TODO
10 // TODO
);
});

Просмотреть файл

@ -1,5 +1,8 @@
{
"require": {
"sabre/xml": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
}
}

980
composer.lock сгенерированный

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -8,5 +8,5 @@ $(document).ready(function () {
jsxc.storage.setItem('sid', '7862');
jsxc.storage.setItem('rid', '897878733');
jsxc.storage.setItem('lastActivity', (new Date()).getTime());
jsxc.storage.setItem('jid', OC.currentUser + OC.getHost());
jsxc.storage.setItem('jid', OC.currentUser + '@' + OC.getHost());
});

Просмотреть файл

@ -60,6 +60,21 @@ class HttpBindController extends Controller {
*/
private $messageHandler;
/**
* @var Body request body
*/
private $body;
/**
* @var SleepTime
*/
private $sleepTime;
/**
* @var SleepTime
*/
private $maxCicles;
public function __construct($appName,
IRequest $request,
$userId,
@ -67,7 +82,11 @@ class HttpBindController extends Controller {
StanzaMapper $stanzaMapper,
IQ $iqHandler,
Message $messageHandler,
$host) {
$host,
$body,
$sleepTime,
$maxCicles
) {
parent::__construct($appName, $request);
$this->userId = $userId;
$this->pollingId = time();
@ -76,6 +95,9 @@ class HttpBindController extends Controller {
$this->host = $host;
$this->iqHandler = $iqHandler;
$this->messageHandler = $messageHandler;
$this->body = $body;
$this->sleepTime = $sleepTime;
$this->maxCicles = $maxCicles;
$this->response = new XMPPResponse();
}
@ -84,7 +106,8 @@ class HttpBindController extends Controller {
* @NoCSRFRequired
*/
public function index() {
$input = file_get_contents('php://input');
$input = $this->body;
$longpoll = true; // set to false when the response should directly be returned and no polling should be done
if (!empty($input)){
// replace invalid XML by valid XML one
$input = str_replace("<vCard xmlns='vcard-temp'/>", "<vCard xmlns='jabber:vcard-temp'/>", $input);
@ -99,14 +122,13 @@ class HttpBindController extends Controller {
} catch (LibXMLException $e){
}
$stanzas = $stanzas['value'];
$longpoll = true; // set to false when the response should directly be returned and no polling should be done
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)){
if (!is_null($result)) {
$longpoll = false;
$this->response->write($result);
}
@ -126,10 +148,10 @@ class HttpBindController extends Controller {
}
$recordFound = true;
} Catch (DoesNotExistException $e) {
sleep(1);
sleep($this->sleepTime);
$recordFound = false;
}
} while ($recordFound === false && $cicles < 10 && $longpoll);
} while ($recordFound === false && $cicles < $this->maxCicles && $longpoll);
return $this->response;
}
@ -149,6 +171,4 @@ class HttpBindController extends Controller {
break;
}
}
}

Просмотреть файл

@ -34,7 +34,7 @@ class IQ extends StanzaHandler {
$iqRoster->setQid($id);
foreach($this->userManager->search('') as $user){
if($user->getUID() !== $this->userId) {
$iqRoster->addItem($user . '@' . $this->host, $user->getDisplayName());
$iqRoster->addItem($user->getUID() . '@' . $this->host, $user->getDisplayName());
}
}
return $iqRoster;

7
phpunit.integration.xml Normal file
Просмотреть файл

@ -0,0 +1,7 @@
<phpunit bootstrap="../../lib/base.php">
<testsuites>
<testsuite name="integration">
<directory>./tests/integration</directory>
</testsuite>
</testsuites>
</phpunit>

7
phpunit.xml Normal file
Просмотреть файл

@ -0,0 +1,7 @@
<phpunit bootstrap="../../lib/base.php">
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>

2
tests/travis/php.ini Normal file
Просмотреть файл

@ -0,0 +1,2 @@
default_charset = "UTF-8"
always_populate_raw_post_data = -1

Просмотреть файл

@ -0,0 +1,117 @@
<?php
namespace OCA\OJSXC\Controller;
use OCA\OJSXC\Db\StanzaMapper;
use OCA\OJSXC\Http\XMPPResponse;
use OCA\OJSXC\StanzaHandlers\IQ;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit_Framework_TestCase;
class HttpBindControllerTest extends PHPUnit_Framework_TestCase {
/**
* @var HttpBindController
*/
private $controller;
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $stanzaMapper;
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $iqHandler;
private $userId = 'john';
public function setUp() {
}
/**
* Helper function to set up the controller. This can't be done in the setUp,
* since the requestBody is different for every test.
* @param $requestBody
*/
private function setUpController($requestBody) {
$request = $this->getMockBuilder('OCP\IRequest')->getMock();
$session = $this->getMockBuilder('OCP\ISession')->getMock();
$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->controller = new HttpBindController(
'ojsxc',
$request,
$this->userId,
$session,
$this->stanzaMapper,
$this->iqHandler,
$messageHandler,
'localhost',
$requestBody,
0,
10
);
}
/**
* When invalid XML, just start long polling.
*/
public function testInvalidXML() {
$ex = new DoesNotExistException();
$expResponse = new XMPPResponse();
$this->setUpController('<x>');
$this->stanzaMapper->expects($this->exactly(10))
->method('findByTo')
->with('john@localhost')
->will($this->throwException($ex));
$response = $this->controller->index();
$this->assertEquals($expResponse, $response);
}
public function IQProvider() {
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>',
$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,
$this->exactly(10)
]
];
}
/**
* @dataProvider IQProvider
*/
public function testIQHandler($body, $result, $pollCount) {
$ex = new DoesNotExistException();
$this->setUpController($body);
$expResponse = new XMPPResponse();
$expResponse->write($result);
$this->iqHandler->expects($this->any()) // FIXME
->method('handle')
->will($this->returnValue($result));
$this->stanzaMapper->expects($pollCount)
->method('findByTo')
->with('john@localhost')
->will($this->throwException($ex));
$response = $this->controller->index();
$this->assertEquals($expResponse, $response);
}
}