Example #1
0
 /**
  * @param string $name
  * @return int
  */
 public function createEntry($name)
 {
     $id = CM_Db_Db::insert('index_mock', array('name' => (string) $name));
     $this->updateDocuments($id);
     $this->refreshIndex();
     return (int) $id;
 }
Example #2
0
 protected static function _createStatic(array $data)
 {
     $user = null;
     $userId = null;
     if (isset($data['user'])) {
         /** @var CM_Model_User $user */
         $user = $data['user'];
         $userId = $user->getId();
     }
     $key = (string) $data['key'];
     $start = (int) $data['start'];
     /** @var CM_Model_StreamChannel_Abstract $streamChannel */
     $streamChannel = $data['streamChannel'];
     if (!$streamChannel->isValid()) {
         throw new CM_Exception_Invalid('Stream channel not valid', null, array('severity' => CM_Exception::WARN));
     }
     $allowedUntil = $streamChannel->canSubscribe($user, time());
     if ($allowedUntil <= time()) {
         throw new CM_Exception_NotAllowed('Not allowed to subscribe');
     }
     $id = CM_Db_Db::insert('cm_stream_subscribe', array('userId' => $userId, 'start' => $start, 'allowedUntil' => $allowedUntil, 'channelId' => $streamChannel->getId(), 'key' => $key));
     $streamSubscribe = new self($id);
     $streamChannel->onSubscribe($streamSubscribe);
     return $streamSubscribe;
 }
Example #3
0
 protected function _load()
 {
     $result = CM_Db_Db::select('cm_model_languagekey', 'name', 'name LIKE ".%"', 'name ASC');
     while ($section = $result->fetch()) {
         $this->_addLanguageNode($section['name']);
     }
 }
Example #4
0
 /**
  * @param string $namespace
  */
 private function _dbToFileSql($namespace)
 {
     $namespace = (string) $namespace;
     $tables = CM_Db_Db::exec("SHOW TABLES LIKE ?", array(strtolower($namespace) . '_%'))->fetchAllColumn();
     sort($tables);
     $dump = CM_Db_Db::getDump($tables, true);
     CM_File::create(CM_Util::getModulePath($namespace) . '/resources/db/structure.sql', $dump);
 }
Example #5
0
 public function findByData($type, array $data)
 {
     $result = CM_Db_Db::select($this->_getTableName($type), array('id'), $data)->fetch();
     if (false === $result) {
         $result = null;
     }
     return $result;
 }
Example #6
0
 public function test_Get()
 {
     $user = CMTest_TH::createUser();
     $user->getRoles()->add(self::ROLE_A, 2000);
     $stamps = CM_Db_Db::select('cm_role', array('startStamp', 'expirationStamp'), array('userId' => $user->getId()))->fetch();
     $this->assertEquals($stamps['startStamp'], $user->getRoles()->getStartStamp(self::ROLE_A));
     $this->assertEquals($stamps['expirationStamp'], $user->getRoles()->getExpirationStamp(self::ROLE_A));
 }
Example #7
0
 public function testTrailingWhitespaceInLanguageKeyName()
 {
     CM_Db_Db::insert('cm_model_languagekey', ['name'], [['foo '], ['foo']]);
     $language = CM_Model_Language::create('Foo', 'foo', true);
     $language->getTranslations()->getAssociativeArray();
     $this->assertEquals(['foo ', 'foo'], array_keys($language->getTranslations()->getAssociativeArray()));
     $this->assertCount(2, $language->getTranslations());
 }
Example #8
0
 /**
  * @param int $age
  */
 public static function deleteOlder($age)
 {
     $age = (int) $age;
     $result = CM_Db_Db::select('cm_tmp_userfile', 'uniqid', '`createStamp` < ' . (time() - $age));
     foreach ($result->fetchAllColumn() as $uniqid) {
         $tmpFile = new CM_File_UserContent_Temp($uniqid);
         $tmpFile->delete();
     }
 }
Example #9
0
 public function reload(CM_OutputStream_Interface $output)
 {
     $tableNames = CM_Db_Db::exec('SHOW TABLES')->fetchAllColumn();
     CM_Db_Db::exec('SET foreign_key_checks = 0;');
     foreach ($tableNames as $table) {
         CM_Db_Db::delete($table);
     }
     CM_Db_Db::exec('SET foreign_key_checks = 1;');
     $this->_setInitialVersion();
 }
Example #10
0
 /**
  * @param string $phrase
  */
 public function remove($phrase)
 {
     $languageKey = CM_Model_LanguageKey::findByName($phrase);
     if (!$languageKey) {
         return;
     }
     CM_Db_Db::delete('cm_languageValue', array('languageKeyId' => $languageKey->getId(), 'languageId' => $this->_language->getId()));
     $this->_change();
     (new self($this->_language, !$this->_javascriptOnly))->_change();
 }
Example #11
0
 public function testDelete()
 {
     $language = CM_Model_Language::create('Foo', 'foo', true);
     $language->setTranslation('foo', 'bar');
     $this->assertSame(array('foo' => array('value' => 'bar', 'variables' => array())), $language->getTranslations()->getAssociativeArray());
     $languageKey = CM_Model_LanguageKey::findByName('foo');
     $languageKey->delete();
     $this->assertSame(array(), $language->getTranslations()->getAssociativeArray());
     $this->assertSame(0, CM_Db_Db::count('cm_model_languagekey', array('name' => 'foo')));
     $this->assertSame(0, CM_Db_Db::count('cm_languageValue', array('languageKeyId' => $languageKey->getId(), 'languageId' => $language->getId())));
 }
Example #12
0
 public function queueOutstanding()
 {
     $executeAtMax = time();
     $result = CM_Db_Db::select('cm_jobdistribution_delayedqueue', '*', '`executeAt` <= ' . $executeAtMax, '`executeAt` ASC');
     while ($row = $result->fetch()) {
         $job = $this->_instantiateJob($row['className']);
         if ($job) {
             $job->queue(CM_Params::decode($row['params'], true));
         }
     }
     CM_Db_Db::delete('cm_jobdistribution_delayedqueue', '`executeAt` <= ' . $executeAtMax);
 }
Example #13
0
 public function testGetInvalidMetaInfo()
 {
     $paging = $this->getMockBuilder('CM_Paging_Log_Abstract')->setMethods(array('getType'))->disableOriginalConstructor()->getMockForAbstractClass();
     $paging->expects($this->any())->method('getType')->will($this->returnValue(14));
     /** @var CM_Paging_Log_Abstract $paging */
     $paging->__construct();
     CM_Db_Db::insert('cm_log', array('msg' => 'foo', 'metaInfo' => str_ireplace('{', '/', serialize(array('foo' => 'bar'))), 'timeStamp' => time(), 'type' => 14));
     $items = $paging->getItems();
     $this->assertSame(1, count($items));
     $this->assertSame('foo', $items[0]['msg']);
     $this->assertSame(null, $items[0]['metaInfo']);
 }
Example #14
0
 public function testPrepare()
 {
     $actor = CMTest_TH::createUser();
     $action = new CM_Action_Mock('foo', $actor);
     $action->prepare(null);
     CM_Db_Db::insert('cm_actionLimit', array('type' => 1, 'actionType' => 1, 'actionVerb' => 1, 'role' => null, 'limit' => 0, 'period' => 0));
     CMTest_TH::clearCache();
     try {
         $action->prepare(null);
         $this->fail('Limited action did not throw exception');
     } catch (CM_Exception_ActionLimit $e) {
         $this->assertSame('Mock overshoot', $e->getMessage());
     }
 }
Example #15
0
 public function testCacheCustom()
 {
     $source = new CM_PagingSource_Sql('`num`', 'test');
     $fileCache = CM_Cache_Persistent::getInstance();
     $source->enableCache(null, $fileCache);
     $this->assertEquals(100, $source->getCount());
     CM_Db_Db::delete('test', array('num' => 0));
     $this->assertEquals(100, $source->getCount());
     $source->clearCache();
     $this->assertEquals(99, $source->getCount());
     CM_Db_Db::delete('test', array('num' => 1));
     $this->assertEquals(99, $source->getCount());
     $fileCache->flush();
     $this->assertEquals(98, $source->getCount());
 }
Example #16
0
 public function testCache()
 {
     $source = new CM_PagingSource_Sql('`num`', 'test');
     $source->enableCache();
     $this->assertSame(100, $source->getCount());
     $sourceNocache = new CM_PagingSource_Sql('`num`', 'test');
     $this->assertSame(100, $sourceNocache->getCount());
     CM_Db_Db::delete('test', array('num' => 0));
     $this->assertSame(100, $source->getCount());
     $this->assertSame(99, $sourceNocache->getCount());
     $source->clearCache();
     $this->assertSame(99, $source->getCount());
     $this->assertSame(99, $sourceNocache->getCount());
     CM_Cache_Shared::getInstance()->flush();
 }
Example #17
0
 protected static function _createStatic(array $data)
 {
     /** @var CM_Model_User $user */
     $user = $data['user'];
     /** @var CM_Model_StreamChannel_Abstract $streamChannel */
     $streamChannel = $data['streamChannel'];
     $start = (int) $data['start'];
     if (!$streamChannel->isValid()) {
         throw new CM_Exception_Invalid('Stream channel not valid', CM_Exception::WARN);
     }
     $allowedUntil = $streamChannel->canPublish($user, time());
     if ($allowedUntil <= time()) {
         throw new CM_Exception_NotAllowed('Not allowed to publish');
     }
     $key = (string) $data['key'];
     $id = CM_Db_Db::insert('cm_stream_publish', array('userId' => $user->getId(), 'start' => $start, 'allowedUntil' => $allowedUntil, 'key' => $key, 'channelId' => $streamChannel->getId()));
     $streamPublish = new self($id);
     $streamChannel->onPublish($streamPublish);
     return $streamPublish;
 }
Example #18
0
 public function testPaging()
 {
     $language = CM_Model_Language::create('Foo', 'foo', true);
     $languagePagingAll = $language->getTranslations();
     $languagePagingJavascriptOnly = $language->getTranslations(true);
     $this->assertEquals([], $languagePagingAll);
     $this->assertEquals([], $languagePagingJavascriptOnly);
     $languagePagingAll->set('foo', 'foo');
     // js
     CM_Db_Db::update('cm_model_languagekey', ['javascript' => 1], ['name' => 'foo']);
     $languagePagingJavascriptOnly->set('bar', 'bar');
     // js
     CM_Db_Db::update('cm_model_languagekey', ['javascript' => 1], ['name' => 'bar']);
     $languagePagingAll->set('baz', 'baz');
     // no js
     $this->assertSame('foo', $language->getTranslations()->get('foo'));
     $this->assertSame('foo', $language->getTranslations(true)->get('foo'));
     $this->assertSame('bar', $language->getTranslations()->get('bar'));
     $this->assertSame('bar', $language->getTranslations(true)->get('bar'));
     $this->assertSame('baz', $language->getTranslations()->get('baz'));
     $exception = $this->catchException(function () use($language) {
         $language->getTranslations(true)->get('baz');
     });
     $this->assertInstanceOf('CM_Exception_Invalid', $exception);
     $languagePagingJavascriptOnly->set('foo', 'bar');
     $this->assertSame('bar', $language->getTranslations(true)->get('foo'));
     $this->assertSame('bar', $language->getTranslations()->get('foo'));
     $languagePagingAll->set('bar', 'foo');
     $this->assertSame('foo', $language->getTranslations()->get('bar'));
     $this->assertSame('foo', $language->getTranslations(true)->get('bar'));
     $languagePagingAll->remove('foo');
     $this->assertSame(null, $language->getTranslations()->get('foo'));
     $this->assertSame(null, $language->getTranslations(true)->get('foo'));
     $languagePagingJavascriptOnly->remove('bar');
     $this->assertSame(null, $language->getTranslations()->get('bar'));
     $this->assertSame(null, $language->getTranslations(true)->get('bar'));
 }
Example #19
0
 /**
  * @param string $id
  * @return array|null
  */
 private static function _findDataById($id)
 {
     $cacheKey = self::_getCacheKey($id);
     $cache = CM_Cache_Shared::getInstance();
     if (($data = $cache->get($cacheKey)) === false) {
         $data = CM_Db_Db::select('cm_session', array('data', 'expires'), array('sessionId' => $id))->fetch();
         if (!$data) {
             return null;
         }
         $cache->set($cacheKey, $data, self::LIFETIME_DEFAULT);
     }
     return $data;
 }
Example #20
0
 protected static function _createStatic(array $data)
 {
     $name = (string) $data['name'];
     $percentage = self::_checkPercentage($data['percentage']);
     CM_Db_Db::insert('cm_splitfeature', array('name' => $name, 'percentage' => $percentage));
     return new static($name);
 }
Example #21
0
 /**
  * @return array
  */
 public static function getStats()
 {
     return CM_Db_Db::exec("SELECT `preferenceId`, COUNT(*) AS `count`, `value` FROM `cm_user_preference` GROUP BY `preferenceId`, `value`")->fetchAllTree();
 }
Example #22
0
File: 46.php Project: cargomedia/cm
<?php

if (!CM_Db_Db::existsColumn('cm_splittest', 'optimized')) {
    CM_Db_Db::exec('
        ALTER TABLE `cm_splittest` ADD COLUMN `optimized` int(1) unsigned NOT NULL AFTER `name`');
}
if (!CM_Db_Db::existsIndex('cm_splittestVariation_fixture', 'splittestVariationCreateStamp')) {
    CM_Db_Db::exec('
        ALTER TABLE `cm_splittestVariation_fixture`
          ADD INDEX `splittestVariationCreateStamp` (`splittestId`,`variationId`,`createStamp`),
          ADD INDEX `splittestVariationConversionStamp` (`splittestId`,`variationId`,`conversionStamp`),
          DROP INDEX `splittestId`,
          DROP INDEX `createStamp`,
          DROP INDEX `conversionStamp`');
}
Example #23
0
 /**
  * @param CM_Model_Location $location
  * @return CM_Model_Currency|null
  */
 public static function findByLocation(CM_Model_Location $location)
 {
     $country = $location->get(CM_Model_Location::LEVEL_COUNTRY);
     if (null === $country) {
         return null;
     }
     $cache = CM_Cache_Local::getInstance();
     $cacheKey = CM_CacheConst::Currency_CountryId . '_countryId:' . $country->getId();
     if (false === ($currencyId = $cache->get($cacheKey))) {
         $currencyId = CM_Db_Db::select('cm_model_currency_country', 'currencyId', ['countryId' => $country->getId()])->fetchColumn();
         $currencyId = $currencyId ? (int) $currencyId : null;
         $cache->set($cacheKey, $currencyId);
     }
     if (null === $currencyId) {
         return null;
     }
     return new self($currencyId);
 }
Example #24
0
 /**
  * @deprecated Usually checking DB rows is not desired, rather test the public interface
  *
  * @param string            $table
  * @param array|string|null $where WHERE conditions: ('attr' => 'value', 'attr2' => 'value')
  * @param int|null          $rowCount
  */
 public static function assertRow($table, $where = null, $rowCount = null)
 {
     if (null === $rowCount) {
         $rowCount = 1;
     }
     $result = CM_Db_Db::select($table, '*', $where);
     $rowCountActual = count($result->fetchAll());
     self::assertEquals($rowCount, $rowCountActual);
 }
Example #25
0
 public function testCustomHeadersQueue()
 {
     $mail = new CM_Mail();
     $subject = uniqid();
     $mail->setSubject($subject);
     $mail->addCustomHeader('X-Foo', 'bar');
     $mail->addCustomHeader('X-Bar', 'foo');
     $mail->addCustomHeader('X-Foo', 'baz');
     $mail->addTo('test');
     $mail->setText('bla');
     $mail->send(true);
     $result = CM_Db_Db::select('cm_mail', 'customHeaders', array('subject' => $subject));
     $row = $result->fetch();
     $this->assertEquals(unserialize($row['customHeaders']), array('X-Foo' => ['bar', 'baz'], 'X-Bar' => ['foo']));
 }
Example #26
0
 public function add(CM_Action_Abstract $action)
 {
     CM_Db_Db::insertDelayed('cm_action', array('actorId' => $this->_user->getId(), 'verb' => $action->getVerb(), 'type' => $action->getType(), 'createStamp' => time()));
 }
Example #27
0
File: Ip.php Project: cargomedia/cm
 public function add(CM_Action_Abstract $action, $limitType)
 {
     $limitType = (int) $limitType;
     CM_Db_Db::insertDelayed('cm_action', array('ip' => $this->_ip, 'verb' => $action->getVerb(), 'type' => $action->getType(), 'actionLimitType' => $limitType, 'createStamp' => time()));
 }
Example #28
0
 public function testOutdatedLocalCache()
 {
     $test1 = CM_Model_Splittest_Mock::create('foo', range(1, 10));
     $test2 = CM_Model_Splittest_Mock::create('bar', range(1, 10));
     $mockBuilder = $this->getMockBuilder('CM_Model_User');
     $mockBuilder->setMethods(['getId']);
     $userMock = $mockBuilder->getMock();
     $userMock->expects($this->any())->method('getId')->will($this->returnValue(mt_rand()));
     /** @var CM_Model_User $userMock */
     $fixture = new CM_Splittest_Fixture($userMock);
     CM_Db_Db::insert('cm_splittestVariation_fixture', array('splittestId' => $test1->getId(), $fixture->getColumnId() => $fixture->getId(), 'variationId' => $test1->getVariations()->findByName(1)->getId(), 'createStamp' => time()));
     $this->assertTrue($test1->isVariationFixture($fixture, 1));
     CM_Db_Db::insert('cm_splittestVariation_fixture', array('splittestId' => $test2->getId(), $fixture->getColumnId() => $fixture->getId(), 'variationId' => $test2->getVariations()->findByName(10)->getId(), 'createStamp' => time()));
     $this->assertTrue($test2->isVariationFixture($fixture, 10));
 }
Example #29
0
 /**
  * @param string $useragent
  */
 public function add($useragent)
 {
     $useragent = (string) $useragent;
     CM_Db_Db::replaceDelayed('cm_useragent', array('userId' => $this->_user->getId(), 'useragent' => $useragent, 'createStamp' => time()));
 }
Example #30
0
 public function testMonitorSynchronizedCommands()
 {
     $mockBuilder = $this->getMockBuilder('CM_Process');
     $mockBuilder->setMethods(['isRunning']);
     $mockBuilder->disableOriginalConstructor();
     $processMock = $mockBuilder->getMock();
     $processMock->expects($this->any())->method('isRunning')->will($this->returnCallback(function ($processId) {
         return $processId !== 3;
     }));
     $mockBuilder = $this->getMockBuilder('CM_Cli_CommandManager');
     $mockBuilder->setMethods(['_getProcess', '_getMachineId']);
     $commandManagerMock = $mockBuilder->getMock();
     $commandManagerMock->expects($this->any())->method('_getProcess')->will($this->returnValue($processMock));
     $commandManagerMock->expects($this->any())->method('_getMachineId')->will($this->returnValue('my-machine-1'));
     CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock1', 'machineId' => 'my-machine-1', 'processId' => 1, 'timeoutStamp' => time() + 60));
     CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock2', 'machineId' => 'my-machine-1', 'processId' => 2, 'timeoutStamp' => time() - 60));
     CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock3', 'machineId' => 'my-machine-1', 'processId' => 3, 'timeoutStamp' => time() + 60));
     CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock4', 'machineId' => 'my-machine-2', 'processId' => 4, 'timeoutStamp' => time() + 60));
     CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock5', 'machineId' => 'my-machine-2', 'processId' => 5, 'timeoutStamp' => time() - 60));
     /** @var CM_Cli_CommandManager $commandManagerMock */
     $commandManagerMock->monitorSynchronizedCommands();
     $timeoutStampExpected = time() + CM_Cli_CommandManager::TIMEOUT;
     $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock1', 'timeoutStamp' => $timeoutStampExpected));
     $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock2', 'timeoutStamp' => $timeoutStampExpected));
     $this->assertNotRow('cm_cli_command_manager_process', array('commandName' => 'command-mock3'));
     $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock4', 'timeoutStamp' => time() + 60));
     $this->assertNotRow('cm_cli_command_manager_process', array('commandName' => 'command-mock5'));
     CM_Db_Db::truncate('cm_cli_command_manager_process');
 }