Пример #1
0
 public function testOnRedisMessageSubscribe()
 {
     $adapter = new CM_MessageStream_Adapter_SocketRedis([]);
     $message = array('type' => 'subscribe', 'data' => array('channel' => 'foo:' . CM_Model_StreamChannel_Message::getTypeStatic(), 'clientKey' => 'bar', 'data' => array()));
     $adapter->onRedisMessage(json_encode($message));
     $timeStarted = time();
     $streamChannel = CM_Model_StreamChannel_Message::findByKeyAndAdapter('foo', $adapter->getType());
     $this->assertNotNull($streamChannel);
     $streamChannels = new CM_Paging_StreamChannel_AdapterType($adapter->getType());
     $this->assertSame(1, $streamChannels->getCount());
     $streamSubscribe = CM_Model_Stream_Subscribe::findByKeyAndChannel('bar', $streamChannel);
     $this->assertNotNull($streamSubscribe);
     $this->assertSame(1, $streamChannel->getStreamSubscribes()->getCount());
     $this->assertSameTime($timeStarted, $streamSubscribe->getStart());
     $this->assertSameTime($streamChannel->canSubscribe(null, time()), $streamSubscribe->getAllowedUntil());
     $this->assertNull($streamSubscribe->getUser());
     CMTest_TH::timeForward(CM_MessageStream_Adapter_SocketRedis::SYNCHRONIZE_DELAY);
     $adapter->onRedisMessage(json_encode($message));
     $streamChannels = new CM_Paging_StreamChannel_AdapterType($adapter->getType());
     $this->assertSame(1, $streamChannels->getCount());
     $this->assertSame(1, $streamChannel->getStreamSubscribes()->getCount());
     CMTest_TH::reinstantiateModel($streamSubscribe);
     $this->assertSameTime($timeStarted, $streamSubscribe->getStart());
     $this->assertSameTime($streamChannel->canSubscribe(null, $timeStarted), $streamSubscribe->getAllowedUntil());
 }
Пример #2
0
 public function testCheckStreams()
 {
     $mockAdapter = $this->getMockForAbstractClass('CM_Stream_Adapter_Video_Abstract', array(), 'CM_Stream_Adapter_Video_Mock', true, true, true, array('_stopStream', 'getType'));
     $mockAdapter->expects($this->exactly(2))->method('_stopStream')->will($this->returnValue(1));
     $mockAdapter->expects($this->any())->method('getType')->will($this->returnValue(1));
     CM_Config::get()->CM_Model_StreamChannel_Abstract->types[CM_Model_StreamChannel_Video_Mock::getTypeStatic()] = 'CM_Model_StreamChannel_Video_Mock';
     $wowza = $wowza = $this->getMock('CM_Stream_Video', array('getAdapter'));
     $wowza->expects($this->any())->method('getAdapter')->will($this->returnValue($mockAdapter));
     /** @var $wowza CM_Stream_Video */
     // allowedUntil will be updated, if stream has expired and its user isn't $userUnchanged, hardcoded in CM_Model_StreamChannel_Video_Mock::canSubscribe() using getOnline()
     $userUnchanged = CMTest_TH::createUser();
     $userUnchanged->setOnline();
     $streamChannel = CM_Model_StreamChannel_Video_Mock::createStatic(array('key' => 'foo1', 'serverId' => 1, 'adapterType' => 1, 'width' => 100, 'height' => 100, 'thumbnailCount' => 0));
     $streamSubscribeUnchanged1 = CM_Model_Stream_Subscribe::createStatic(array('streamChannel' => $streamChannel, 'user' => $userUnchanged, 'key' => 'foo1_2', 'start' => time()));
     $streamSubscribeUnchanged2 = CM_Model_Stream_Subscribe::createStatic(array('streamChannel' => $streamChannel, 'user' => CMTest_TH::createUser(), 'key' => 'foo1_4', 'start' => time()));
     $streamSubscribeChanged1 = CM_Model_Stream_Subscribe::createStatic(array('streamChannel' => $streamChannel, 'user' => CMTest_TH::createUser(), 'key' => 'foo1_3', 'start' => time()));
     $streamPublishUnchanged1 = CM_Model_Stream_Publish::createStatic(array('streamChannel' => $streamChannel, 'user' => $userUnchanged, 'key' => 'foo1_2', 'start' => time()));
     $streamPublishChanged1 = CM_Model_Stream_Publish::createStatic(array('streamChannel' => CM_Model_StreamChannel_Video_Mock::createStatic(array('key' => 'foo2', 'serverId' => 1, 'adapterType' => 1, 'width' => 100, 'height' => 100, 'thumbnailCount' => 0)), 'user' => CMTest_TH::createUser(), 'key' => 'foo2_1', 'start' => time()));
     $this->assertSameTime($streamSubscribeUnchanged1->getAllowedUntil(), time() + 10);
     $this->assertSameTime($streamSubscribeUnchanged2->getAllowedUntil(), time() + 100);
     $this->assertSameTime($streamSubscribeChanged1->getAllowedUntil(), time() + 100);
     $this->assertSameTime($streamPublishUnchanged1->getAllowedUntil(), time() + 10);
     $this->assertSameTime($streamPublishChanged1->getAllowedUntil(), time() + 100);
     CMTest_TH::timeForward(200);
     $wowza->checkStreams();
     $this->assertEquals($streamSubscribeUnchanged1->getAllowedUntil() + 10, $streamSubscribeUnchanged1->_change()->getAllowedUntil());
     $this->assertEquals($streamSubscribeUnchanged2->getAllowedUntil() + 100, $streamSubscribeUnchanged2->_change()->getAllowedUntil());
     $this->assertEquals($streamSubscribeChanged1->getAllowedUntil() + 100, $streamSubscribeChanged1->_change()->getAllowedUntil());
     $this->assertEquals($streamPublishUnchanged1->getAllowedUntil() + 10, $streamPublishUnchanged1->_change()->getAllowedUntil());
     $this->assertEquals($streamPublishChanged1->getAllowedUntil() + 100, $streamPublishChanged1->_change()->getAllowedUntil());
 }
Пример #3
0
 public function testClean()
 {
     $user1 = CMTest_TH::createUser();
     $user2 = CMTest_TH::createUser();
     $user1->getRoles()->add(self::ROLE_A, 2000);
     $user1->getRoles()->add(self::ROLE_C);
     $user1->getRoles()->add(self::ROLE_B, 1000);
     $user2->getRoles()->add(self::ROLE_A, 2000);
     $user2->getRoles()->add(self::ROLE_B, 1000);
     $this->assertTrue($user1->getRoles()->contains(self::ROLE_B));
     CMTest_TH::timeForward(1500);
     CM_ModelAsset_User_Roles::deleteOld($user1);
     $user1->_change();
     $user2->_change();
     $this->assertFalse($user1->getRoles()->contains(self::ROLE_B));
     $this->assertRow('cm_role', array('userId' => $user2->getId(), 'role' => self::ROLE_B));
     $this->assertFalse($user2->getRoles()->contains(self::ROLE_B));
     $this->assertTrue($user2->getRoles()->contains(self::ROLE_A));
     CM_ModelAsset_User_Roles::deleteOld();
     $user1->_change();
     $user2->_change();
     $this->assertNotRow('cm_role', array('userId' => $user2->getId(), 'role' => self::ROLE_B));
     $this->assertTrue($user2->getRoles()->contains(self::ROLE_A));
     $this->assertTrue($user1->getRoles()->contains(self::ROLE_A));
     $this->assertTrue($user1->getRoles()->contains(self::ROLE_C));
 }
Пример #4
0
 public function testCleanUp()
 {
     $client = $this->getServiceManager()->getMongoDb();
     $encoder = new CM_Log_Encoder_MongoDb();
     $handler = new CM_Log_Handler_MongoDb($client, $encoder, CM_Paging_Log::COLLECTION_NAME);
     $context1 = new CM_Log_Context();
     $context1->setExtra(['bar' => 'quux']);
     $record1 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'foo', $context1);
     $record2 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'baz', new CM_Log_Context());
     $record3 = new CM_Log_Record(CM_Log_Logger::CRITICAL, 'bar', new CM_Log_Context());
     $record4 = new CM_Log_Record(CM_Log_Logger::INFO, 'bazBar', new CM_Log_Context());
     $contextTyped = new CM_Log_Context();
     $contextTyped->setExtra(['type' => 1]);
     $typedRecord = new CM_Log_Record(CM_Log_Logger::DEBUG, 'quux', $contextTyped);
     $paging = new CM_Paging_Log([CM_Log_Logger::DEBUG, CM_Log_Logger::INFO]);
     $this->assertSame(0, $paging->getCount());
     $handler->handleRecord($record1);
     $handler->handleRecord($record2);
     $handler->handleRecord($record3);
     $handler->handleRecord($record4);
     $handler->handleRecord($typedRecord);
     $paging->_change();
     $this->assertSame(4, $paging->getCount());
     $age = 7 * 86400 + 1;
     CMTest_TH::timeForward($age);
     $paging->cleanUp();
     $this->assertSame(0, $paging->getCount());
 }
Пример #5
0
 public function testDeleteOlder()
 {
     $file = CM_File_UserContent_Temp::create('foo.txt', 'bar');
     $this->assertTrue($file->exists());
     CM_File_UserContent_Temp::deleteOlder(100);
     $this->assertTrue($file->exists());
     CMTest_TH::timeForward(1000);
     CM_File_UserContent_Temp::deleteOlder(100);
     $this->assertFalse($file->exists());
 }
Пример #6
0
 public function testDeleteExpired()
 {
     CMTest_TH::timeInit();
     $runtime = new CM_Cache_Storage_Runtime();
     $runtime->set('foo', true);
     $this->assertTrue($runtime->get('foo'));
     CMTest_TH::timeForward(5);
     $runtime->set('bar', true);
     $this->assertFalse($runtime->get('foo'));
 }
Пример #7
0
 public function testDeleteOlder()
 {
     $captcha = CM_Captcha::create();
     $id = $captcha->getId();
     CMTest_TH::timeForward(200);
     CM_Captcha::deleteOlder(100);
     try {
         new CM_Captcha($id);
         $this->fail('Can construct old captcha');
     } catch (CM_Exception_Nonexistent $e) {
         $this->assertTrue(true);
     }
 }
Пример #8
0
 public function testPaging()
 {
     $key = 'foo';
     $streamChannel1 = CMTest_TH::createStreamChannel();
     $streamChannel1->_set('key', $key);
     $streamChannel2 = CMTest_TH::createStreamChannel();
     $streamChannel2->_set('key', $key);
     $this->assertEquals([], new CM_Paging_StreamChannelArchiveMedia_Key($key));
     $streamChannelArchive1 = CMTest_TH::createStreamChannelVideoArchive($streamChannel1);
     CMTest_TH::timeForward(1);
     $streamChannelArchive2 = CMTest_TH::createStreamChannelVideoArchive($streamChannel2);
     $this->assertEquals([$streamChannelArchive1, $streamChannelArchive2], new CM_Paging_StreamChannelArchiveMedia_Key($key));
 }
Пример #9
0
 public function testPaging()
 {
     $user = CMTest_TH::createUser();
     CMTest_TH::createStreamChannelVideoArchive(null, $user);
     CMTest_TH::createStreamChannelVideoArchive(null, $user);
     CMTest_TH::timeForward(1);
     $streamChannel3 = CMTest_TH::createStreamChannelVideoArchive(null, $user);
     CMTest_TH::createStreamChannelVideoArchive();
     CMTest_TH::createStreamChannelVideoArchive();
     $paging = new CM_Paging_StreamChannelArchiveVideo_User($user);
     $this->assertSame(3, $paging->getCount());
     $this->assertEquals($streamChannel3, $paging->getItem(0));
 }
Пример #10
0
 public function testReconnectTimeout()
 {
     $config = CM_Service_Manager::getInstance()->getDatabases()->getMaster()->getConfig();
     $config['reconnectTimeout'] = 5;
     $client = new CM_Db_Client($config);
     $client->connect();
     $firstTime = $client->getLastConnect();
     $timeForward = 100;
     CMTest_TH::timeForward($timeForward);
     $client->createStatement('SELECT 1')->execute();
     $this->assertSameTime($firstTime + $timeForward, $client->getLastConnect(), 5);
     CMTest_TH::timeForward($timeForward);
     $client->createStatement('SELECT 1')->execute();
     $this->assertSameTime($firstTime + 2 * $timeForward, $client->getLastConnect(), 5);
 }
Пример #11
0
 public function testAdd()
 {
     $user = CMTest_TH::createUser();
     $useragentList = new CM_Paging_Useragent_User($user);
     $time = time();
     $this->assertSame([], (new CM_Paging_Useragent_User($user))->getItems());
     $useragentList->add('Mozilla');
     $this->assertSame([['useragent' => 'Mozilla', 'createStamp' => $time]], (new CM_Paging_Useragent_User($user))->getItems());
     CMTest_TH::timeForward(1);
     $useragentList->add('Internet Explorer');
     $this->assertSame([['useragent' => 'Internet Explorer', 'createStamp' => $time + 1], ['useragent' => 'Mozilla', 'createStamp' => $time]], (new CM_Paging_Useragent_User($user))->getItems());
     CMTest_TH::timeForward(1);
     $useragentList->add('Mozilla');
     $this->assertSame([['useragent' => 'Mozilla', 'createStamp' => $time + 2], ['useragent' => 'Internet Explorer', 'createStamp' => $time + 1]], (new CM_Paging_Useragent_User($user))->getItems());
 }
Пример #12
0
 public function testCleanUp()
 {
     $paging = $this->getMockBuilder('CM_Paging_Log_Abstract')->disableOriginalConstructor()->setMethods(array('getType'))->getMockForAbstractClass();
     $paging->expects($this->any())->method('getType')->will($this->returnValue(1));
     /** @var CM_Paging_Log_Abstract $paging */
     $paging->__construct();
     $this->assertSame(0, $paging->getCount());
     CMTest_TH::getProtectedMethod($paging, '_add')->invoke($paging, 'foo');
     $paging->_change();
     $this->assertSame(1, $paging->getCount());
     $age = 7 * 86400;
     CMTest_TH::timeForward($age);
     $paging->cleanUp();
     $this->assertSame(0, $paging->getCount());
 }
Пример #13
0
 public function testPaging()
 {
     CMTest_TH::createStreamChannelVideoArchive();
     $archive = CMTest_TH::createStreamChannelVideoArchive();
     CMTest_TH::timeForward(30);
     CMTest_TH::createStreamChannelVideoArchive();
     /** @var CM_Model_StreamChannel_Video $streamChannel */
     $streamChannel = CMTest_TH::createStreamChannel();
     $streamChannel = $this->getMock('CM_Model_StreamChannel_Video', array('getType'), array($streamChannel->getId()));
     $streamChannel->expects($this->any())->method('getType')->will($this->returnValue(3));
     CMTest_TH::createStreamChannelVideoArchive($streamChannel);
     $paging = new CM_Paging_StreamChannelArchiveVideo_Type(CM_Model_StreamChannel_Video::getTypeStatic());
     $this->assertSame(3, $paging->getCount());
     $paging = new CM_Paging_StreamChannelArchiveVideo_Type($streamChannel->getType());
     $this->assertSame(1, $paging->getCount());
     $paging = new CM_Paging_StreamChannelArchiveVideo_Type(CM_Model_StreamChannel_Video::getTypeStatic(), $archive->getCreated());
     $this->assertSame(2, $paging->getCount());
 }
Пример #14
0
 public function testFlushVariationCache()
 {
     $test = CM_Model_Splittest::create('foo', ['v1', 'v2']);
     $variation1 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v1'])->fetchColumn());
     $variation2 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v2'])->fetchColumn());
     $variation2->setEnabled(false);
     $fixture = $this->mockClass('CM_Splittest_Fixture')->newInstanceWithoutConstructor();
     $fixture->mockMethod('getId')->set(1);
     $fixture->mockMethod('getFixtureType')->set(1);
     CMTest_TH::timeForward(1);
     $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]);
     $this->assertSame('v1', $variation);
     $test->flush();
     $variation2->setEnabled(true);
     $variation1->setEnabled(false);
     $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]);
     $this->assertSame('v2', $variation);
 }
Пример #15
0
 public function testSynchronizeMissingInWowza()
 {
     $streamChannel = CMTest_TH::createStreamChannel();
     $streamPublish = CMTest_TH::createStreamPublish(null, $streamChannel);
     $streamSubscribe = CMTest_TH::createStreamSubscribe(null, $streamChannel);
     $wowza = $this->getMock('CM_Stream_Adapter_Video_Wowza', array('_fetchStatus'));
     $json = $this->_generateWowzaData(array());
     $wowza->expects($this->any())->method('_fetchStatus')->will($this->returnValue($json));
     /** @var $wowza CM_Stream_Video */
     $wowza->synchronize();
     $this->assertEquals($streamChannel, CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamChannel->getKey(), $wowza->getType()));
     $this->assertEquals($streamPublish, CM_Model_Stream_Publish::findByKeyAndChannel($streamPublish->getKey(), $streamChannel));
     $this->assertEquals($streamSubscribe, CM_Model_Stream_Subscribe::findByKeyAndChannel($streamSubscribe->getKey(), $streamChannel));
     CMTest_TH::timeForward(5);
     $wowza->synchronize();
     $this->assertNull(CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamChannel->getKey(), $wowza->getType()));
     $this->assertNull(CM_Model_Stream_Publish::findByKeyAndChannel($streamPublish->getKey(), $streamChannel));
     $this->assertNull(CM_Model_Stream_Subscribe::findByKeyAndChannel($streamSubscribe->getKey(), $streamChannel));
 }
Пример #16
0
 public function testPaging()
 {
     CMTest_TH::createStreamChannelVideoArchive();
     $archive = CMTest_TH::createStreamChannelVideoArchive();
     CMTest_TH::timeForward(30);
     CMTest_TH::createStreamChannelVideoArchive();
     /** @var CM_Model_StreamChannel_Media $streamChannel */
     $streamChannel = CMTest_TH::createStreamChannel();
     $mockBuilder = $this->getMockBuilder('CM_Model_StreamChannel_Media');
     $mockBuilder->setMethods(['getType']);
     $mockBuilder->setConstructorArgs([$streamChannel->getId()]);
     $streamChannelMock = $mockBuilder->getMock();
     $streamChannelMock->expects($this->any())->method('getType')->will($this->returnValue(3));
     CMTest_TH::createStreamChannelVideoArchive($streamChannelMock);
     $paging = new CM_Paging_StreamChannelArchiveMedia_Type(CM_Model_StreamChannel_Media::getTypeStatic());
     $this->assertSame(3, $paging->getCount());
     $paging = new CM_Paging_StreamChannelArchiveMedia_Type($streamChannelMock->getType());
     $this->assertSame(1, $paging->getCount());
     $paging = new CM_Paging_StreamChannelArchiveMedia_Type(CM_Model_StreamChannel_Media::getTypeStatic(), $archive->getCreated());
     $this->assertSame(2, $paging->getCount());
 }
Пример #17
0
 public function testCancelJob()
 {
     /** @var CM_Jobdistribution_Job_Abstract|\Mocka\AbstractClassTrait $job */
     $jobToExec = $this->mockObject('CM_Jobdistribution_Job_Abstract');
     /** @var CM_Jobdistribution_Job_Abstract|\Mocka\AbstractClassTrait $job */
     $jobToCancel = $this->mockObject('CM_Jobdistribution_Job_Abstract');
     $user = CMTest_TH::createUser();
     $params1 = ['foo' => 1, 'bar' => $user];
     $params2 = ['foo' => 2, 'bar' => $user];
     $queueExecMethod = $jobToExec->mockMethod('queue')->set(function (array $params) use($params1) {
         $this->assertEquals($params1, $params);
     });
     $queueCancelMethod = $jobToCancel->mockMethod('queue');
     /** @var CM_Jobdistribution_DelayedQueue|\Mocka\AbstractClassTrait $delayedQueue */
     $delayedQueue = $this->mockObject('CM_Jobdistribution_DelayedQueue', [$this->getServiceManager()]);
     /** @var \Mocka\FunctionMock $instantiateMethod */
     $instantiateMethod = $delayedQueue->mockMethod('_instantiateJob')->set(function ($className) use($jobToExec, $jobToCancel) {
         $job = null;
         if ($className === get_class($jobToExec)) {
             $job = $jobToExec;
         } elseif ($className === get_class($jobToCancel)) {
             $job = $jobToCancel;
         }
         $this->assertNotNull($job);
         return $job;
     });
     $delayedQueue->addJob($jobToExec, $params1, 2);
     $delayedQueue->addJob($jobToCancel, $params2, 2);
     CMTest_TH::timeForward(1);
     $delayedQueue->queueOutstanding();
     $this->assertSame(0, $instantiateMethod->getCallCount());
     $this->assertSame(0, $queueExecMethod->getCallCount());
     $this->assertSame(0, $queueCancelMethod->getCallCount());
     $delayedQueue->cancelJob($jobToCancel, $params2);
     CMTest_TH::timeForward(2);
     $delayedQueue->queueOutstanding();
     $this->assertSame(1, $instantiateMethod->getCallCount());
     $this->assertSame(1, $queueExecMethod->getCallCount());
     $this->assertSame(0, $queueCancelMethod->getCallCount());
 }
Пример #18
0
 public function testAddDelete()
 {
     $logger = CM_Service_Manager::getInstance()->getLogger();
     $this->assertSame(0, (new CM_Paging_Log_Mail([CM_Log_Logger::INFO]))->getCount());
     $this->assertSame(0, (new CM_Paging_Log([CM_Log_Logger::INFO]))->getCount());
     $context1 = new CM_Log_Context();
     $context1->setExtra(['type' => CM_Paging_Log_Mail::getTypeStatic(), 'foo' => 'foo']);
     $logger->info('mail foo', $context1);
     $context2 = new CM_Log_Context();
     $context2->setExtra(['type' => CM_Paging_Log_Mail::getTypeStatic(), 'bar' => 'bar']);
     $logger->info('mail bar', $context2);
     $context3 = new CM_Log_Context();
     $context3->setExtra(['baz' => 'baz']);
     $logger->info('not mail', $context3);
     $this->assertSame(2, (new CM_Paging_Log_Mail([CM_Log_Logger::INFO]))->getCount());
     $this->assertSame(3, (new CM_Paging_Log([CM_Log_Logger::INFO]))->getCount());
     $age = 7 * 86400 + 1;
     CMTest_TH::timeForward($age);
     (new CM_Paging_Log_Mail([CM_Log_Logger::INFO]))->cleanUp();
     $this->assertSame(0, (new CM_Paging_Log_Mail([CM_Log_Logger::INFO]))->getCount());
     $this->assertSame(1, (new CM_Paging_Log([CM_Log_Logger::INFO]))->getCount());
 }
Пример #19
0
 public function testUpdateLatestActivityThrottled()
 {
     $user = CMTest_TH::createUser();
     $activityStamp1 = time();
     $this->assertSameTime($activityStamp1, $user->getLatestActivity());
     CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2);
     $user->updateLatestActivityThrottled();
     $this->assertSameTime($activityStamp1, $user->getLatestActivity());
     CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2 + 1);
     $activityStamp2 = time();
     $user->updateLatestActivityThrottled();
     $this->assertSameTime($activityStamp2, $user->getLatestActivity());
 }
Пример #20
0
 public function testDeleteOlder()
 {
     $archivesDeleted = [];
     $archivesNotDeleted = [];
     for ($i = 0; $i < 2; $i++) {
         $archive = CMTest_TH::createStreamChannelVideoArchive();
         $archivesDeleted[] = $archive;
     }
     $streamChannel = CMTest_TH::createStreamChannel();
     $mockBuilder = $this->getMockBuilder('CM_Model_StreamChannel_Media');
     $mockBuilder->setMethods(['getType']);
     $mockBuilder->setConstructorArgs([$streamChannel->getId()]);
     $streamChannelMock = $mockBuilder->getMock();
     $streamChannelMock->expects($this->any())->method('getType')->will($this->returnValue(3));
     $archive = CMTest_TH::createStreamChannelVideoArchive($streamChannelMock);
     $archivesNotDeleted[] = $archive;
     CMTest_TH::timeForward(20);
     for ($i = 0; $i < 3; $i++) {
         $archive = CMTest_TH::createStreamChannelVideoArchive();
         $archivesNotDeleted[] = $archive;
     }
     $this->assertCount(6, new CM_Paging_StreamChannelArchiveMedia_All());
     CM_Model_StreamChannelArchive_Media::deleteOlder(10, CM_Model_StreamChannel_Media::getTypeStatic());
     $this->assertCount(4, new CM_Paging_StreamChannelArchiveMedia_All());
     foreach ($archivesNotDeleted as $archive) {
         $exception = $this->catchException(function () use($archive) {
             CMTest_TH::reinstantiateModel($archive);
         });
         $this->assertNull($exception, 'Deleted archive that was too young or the wrong type');
     }
     foreach ($archivesDeleted as $archive) {
         $exception = $this->catchException(function () use($archive) {
             CMTest_TH::reinstantiateModel($archive);
         });
         $this->assertInstanceOf('CM_Exception_Nonexistent', $exception, 'Didn\'t delete old archive');
     }
 }
Пример #21
0
 public function testTrainChanged()
 {
     $svm = new CM_SVM_Model(1);
     CMTest_TH::timeInit();
     CMTest_TH::timeForward(10);
     $svm->addTraining(-1, array(1 => 1.0, 2 => 0.0));
     $svm->addTraining(1, array(1 => 0.0, 2 => 1.0));
     $this->assertNotSame(1, $svm->predict(array(1 => 0.0, 2 => 1.0)));
     CM_SVM_Model::trainChanged();
     $svm = new CM_SVM_Model(1);
     $this->assertSame(1, $svm->predict(array(1 => 0.0, 2 => 1.0)));
     $svm->flush();
 }
Пример #22
0
 public function testDeleteOlder()
 {
     $time = time();
     /** @var CM_Model_StreamChannel_Video $streamChannel */
     $streamChannelsDeleted = array();
     $archivesDeleted = array();
     /** @var $filesDeleted CM_File[] */
     $filesDeleted = array();
     for ($i = 0; $i < 2; $i++) {
         $streamChannel = CMTest_TH::createStreamChannel();
         $streamChannel->setThumbnailCount(4);
         $streamChannelsDeleted[] = $streamChannel;
         $archive = CMTest_TH::createStreamChannelVideoArchive($streamChannel);
         $archivesDeleted[] = $archive;
         $filesDeleted = array_merge($filesDeleted, $this->_createArchiveFiles($archive));
     }
     $streamChannelsNotDeleted = array();
     $archivesNotDeleted = array();
     /** @var $filesNotDeleted CM_File[] */
     $filesNotDeleted = array();
     $streamChannel = CMTest_TH::createStreamChannel();
     $streamChannel = $this->getMock('CM_Model_StreamChannel_Video', array('getType'), array($streamChannel->getId()));
     $streamChannel->expects($this->any())->method('getType')->will($this->returnValue(3));
     $streamChannelsNotDeleted[] = $streamChannel;
     $archive = CMTest_TH::createStreamChannelVideoArchive($streamChannel);
     $archivesNotDeleted[] = $archive;
     $filesNotDeleted = array_merge($filesNotDeleted, $this->_createArchiveFiles($archive));
     CMTest_TH::timeForward(20);
     for ($i = 0; $i < 3; $i++) {
         $streamChannel = CMTest_TH::createStreamChannel();
         $streamChannel->setThumbnailCount(4);
         $streamChannelsNotDeleted[] = $streamChannel;
         $archive = CMTest_TH::createStreamChannelVideoArchive($streamChannel);
         $archivesNotDeleted[] = $archive;
         $filesNotDeleted = array_merge($filesNotDeleted, $this->_createArchiveFiles($archive));
     }
     foreach ($filesNotDeleted as $file) {
         $this->assertTrue($file->exists());
     }
     foreach ($filesDeleted as $file) {
         $this->assertTrue($file->exists());
     }
     CM_Model_StreamChannelArchive_Video::deleteOlder(10, CM_Model_StreamChannel_Video::getTypeStatic());
     foreach ($filesNotDeleted as $file) {
         $this->assertTrue($file->exists());
     }
     foreach ($filesDeleted as $file) {
         $this->assertFalse($file->exists());
     }
     foreach ($archivesNotDeleted as $archive) {
         try {
             CMTest_TH::reinstantiateModel($archive);
             $this->assertTrue(true);
         } catch (CM_Exception_Nonexistent $ex) {
             $this->fail('Young streamchannelArchive deleted');
         }
     }
     foreach ($archivesDeleted as $archive) {
         try {
             CMTest_TH::reinstantiateModel($archive);
             $this->fail('Old streamchannelArchive not deleted');
         } catch (CM_Exception_Nonexistent $ex) {
             $this->assertTrue(true);
         }
     }
 }
Пример #23
0
 public function testOfflineDelayed()
 {
     $user1 = CMTest_TH::createUser();
     $user2 = CMTest_TH::createUser();
     $user3 = CMTest_TH::createUser();
     $user1->setOnline();
     $user2->setOnline();
     $user3->setOnline();
     $user1->setOfflineStamp();
     $user2->setOfflineStamp();
     CMTest_TH::timeForward(CM_Model_User::OFFLINE_DELAY);
     $user3->setOfflineStamp();
     $user2->setOnline();
     $userOnlinePaging = new CM_Paging_User_Online();
     $this->assertEquals([$user1, $user2, $user3], $userOnlinePaging);
     CM_Model_User::offlineDelayed();
     $userOnlinePaging = new CM_Paging_User_Online();
     $this->assertEquals([$user1, $user2, $user3], $userOnlinePaging);
     CMTest_TH::timeForward(1);
     CM_Model_User::offlineDelayed();
     $userOnlinePaging = new CM_Paging_User_Online();
     $this->assertEquals([$user2, $user3], $userOnlinePaging);
 }
Пример #24
0
 public function testSynchronizeMissingInJanus()
 {
     $streamChannel = CMTest_TH::createStreamChannel(null, CM_Janus_Service::getTypeStatic());
     $streamPublish = CMTest_TH::createStreamPublish(null, $streamChannel);
     $streamSubscribe = CMTest_TH::createStreamSubscribe(null, $streamChannel);
     $existingStreamChannel = CMTest_TH::createStreamChannel(null, CM_Janus_Service::getTypeStatic());
     $existingStreamPublish = CMTest_TH::createStreamPublish(null, $existingStreamChannel);
     $existingStreamSubscribe = CMTest_TH::createStreamSubscribe(null, $existingStreamChannel);
     $emptyStreamChannel = CMTest_TH::createStreamChannel(null, CM_Janus_Service::getTypeStatic());
     $location = $this->mockClass('CM_Geo_Point')->newInstanceWithoutConstructor();
     $server1 = $this->mockClass('CM_Janus_Server')->newInstance([1, 'key', 'http://mock', 'ws://mock', [], $location]);
     /** @var CM_Janus_ServerList|\Mocka\AbstractClassTrait $serverList */
     $serverList = $this->mockObject('CM_Janus_ServerList');
     $serverList->mockMethod('getAll')->set([$server1]);
     /** @var CM_Janus_HttpApiClient|\Mocka\AbstractClassTrait $httpApiClient */
     $httpApiClient = $this->mockClass('CM_Janus_HttpApiClient')->newInstanceWithoutConstructor();
     $httpApiClient->mockMethod('fetchStatus')->set([['id' => $existingStreamPublish->getKey(), 'channelName' => $existingStreamChannel->getKey(), 'isPublish' => true], ['id' => $existingStreamSubscribe->getKey(), 'channelName' => $existingStreamChannel->getKey(), 'isPublish' => false]]);
     $janus = new CM_Janus_Service($serverList, $httpApiClient);
     $janus->synchronize();
     $this->assertEquals($streamChannel, CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamChannel->getKey(), $janus->getType()));
     $this->assertEquals($streamPublish, CM_Model_Stream_Publish::findByKeyAndChannel($streamPublish->getKey(), $streamChannel));
     $this->assertEquals($streamSubscribe, CM_Model_Stream_Subscribe::findByKeyAndChannel($streamSubscribe->getKey(), $streamChannel));
     $this->assertEquals($existingStreamChannel, CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($existingStreamChannel->getKey(), $janus->getType()));
     $this->assertEquals($existingStreamPublish, CM_Model_Stream_Publish::findByKeyAndChannel($existingStreamPublish->getKey(), $existingStreamChannel));
     $this->assertEquals($existingStreamSubscribe, CM_Model_Stream_Subscribe::findByKeyAndChannel($existingStreamSubscribe->getKey(), $existingStreamChannel));
     $this->assertNull(CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($emptyStreamChannel->getKey(), $janus->getType()));
     CMTest_TH::timeForward(5);
     $janus->synchronize();
     $this->assertNull(CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamChannel->getKey(), $janus->getType()));
     $this->assertNull(CM_Model_Stream_Publish::findByKeyAndChannel($streamPublish->getKey(), $streamChannel));
     $this->assertNull(CM_Model_Stream_Subscribe::findByKeyAndChannel($streamSubscribe->getKey(), $streamChannel));
     $this->assertEquals($existingStreamChannel, CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($existingStreamChannel->getKey(), $janus->getType()));
     $this->assertEquals($existingStreamPublish, CM_Model_Stream_Publish::findByKeyAndChannel($existingStreamPublish->getKey(), $existingStreamChannel));
     $this->assertEquals($existingStreamSubscribe, CM_Model_Stream_Subscribe::findByKeyAndChannel($existingStreamSubscribe->getKey(), $existingStreamChannel));
 }
Пример #25
0
 public function testStart()
 {
     /** @var CM_Model_User $user */
     $user = CM_Model_User::createStatic();
     $activityStamp1 = time();
     $session = new CM_Session();
     $session->setUser($user);
     $sessionId = $session->getId();
     unset($session);
     $session = new CM_Session($sessionId);
     $this->assertEquals($activityStamp1, $session->getUser(true)->getLatestActivity(), null, 1);
     CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2);
     $session = new CM_Session($sessionId);
     $session->start();
     $this->assertEquals($activityStamp1, $session->getUser(true)->getLatestActivity(), null, 1);
     CM_Db_Db::update('cm_session', array('data' => serialize(array('userId' => $user->getId(), 'foo' => 'bar'))));
     unset($session);
     CMTest_TH::clearCache();
     CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2 + 1);
     $activityStamp2 = time();
     $session = new CM_Session($sessionId);
     $session->start();
     $this->assertEquals($activityStamp2, $session->getUser(true)->getLatestActivity(), null, 1);
     CMTest_TH::timeForward($session->getLifetime() / 2);
     $session->start();
     $this->assertEquals('bar', $session->get('foo'));
     CM_Db_Db::update('cm_session', array('data' => serialize(array('userId' => $user->getId(), 'foo' => 'foo'))));
     unset($session);
     CMTest_TH::clearCache();
     $session = new CM_Session($sessionId);
     $this->assertEquals('bar', $session->get('foo'));
 }
Пример #26
0
 public function testDeleteTransgressionsOlder()
 {
     $user = CMTest_TH::createUser();
     $action = $this->mockClass('CM_Action_Abstract')->newInstanceWithoutConstructor();
     $action->mockMethod('getType')->set(function () {
         return 1;
     });
     $action->mockMethod('getVerb')->set(function () {
         return 2;
     });
     /** @var CM_Action_Abstract $action */
     $transgressions = $user->getTransgressions();
     $actions = $user->getActions();
     $transgressions->add($action, 1);
     $transgressions->add($action, 2);
     $actions->add($action, 1);
     CMTest_TH::timeForward(61);
     CM_Action_Abstract::deleteTransgressionsOlder(60);
     $transgressions->add($action, 3);
     $this->assertCount(1, $actions);
     $this->assertCount(1, $transgressions);
     CMTest_TH::timeForward(61);
     CM_Action_Abstract::deleteTransgressionsOlder(60);
     $transgressions->_change();
     $this->assertCount(0, $transgressions);
 }