public function testCreateAndGetGameCollectionById()
 {
     $user = UserTestHelper::createBasicUser('Steven');
     $gameCollection = new GameCollection();
     $gameCollection->person = $user;
     $gameCollection->type = 'Basketball';
     $gameCollection->serializedData = serialize(array('something'));
     $this->assertTrue($gameCollection->save());
     $id = $gameCollection->id;
     unset($gameCollection);
     $gameCollection = GameCollection::getById($id);
     $this->assertEquals('Basketball', $gameCollection->type);
     $this->assertEquals($user, $gameCollection->person);
     $this->assertEquals(array('something'), unserialize($gameCollection->serializedData));
 }
 public function testRemoveDuplicatesByModelsGameCollection()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $models = array();
     $gameCollection = new GameCollection();
     $gameCollection->person = $super;
     $gameCollection->type = 'Basketball';
     $gameCollection->serializedData = serialize(array('something'));
     $gameCollection->save();
     $models[] = $gameCollection;
     $gameCollection2 = new GameCollection();
     $gameCollection2->person = $super;
     $gameCollection2->type = 'Basketball';
     $gameCollection2->serializedData = serialize(array('something2'));
     $gameCollection2->save();
     $models[] = $gameCollection2;
     $messageContent = null;
     $this->assertEquals(2, count(GameCollection::getAll()));
     GamificationUtil::removeDuplicatesByModels($models, $messageContent);
     $gameCollections = GameCollection::getAll();
     $this->assertEquals(1, count($gameCollections));
     $this->assertNotNull($messageContent);
 }
 public function testRedeemCollection()
 {
     $user = UserTestHelper::createBasicUser('Steven');
     $gameCollection = new GameCollection();
     $gameCollection->person = $user;
     $gameCollection->type = 'Basketball';
     $itemsData = array('RedemptionItem' => 0, 'Items' => array('Backboard' => 0, 'Player' => 0, 'ScoreBoard' => 0, 'Uniform' => 0, 'Trophy' => 0));
     $gameCollection->serializedData = serialize($itemsData);
     $this->assertTrue($gameCollection->save());
     $id = $gameCollection->id;
     unset($gameCollection);
     $gameCollection = GameCollection::getById($id);
     $this->assertEquals('Basketball', $gameCollection->type);
     $this->assertEquals($user, $gameCollection->person);
     $this->assertEquals($itemsData, unserialize($gameCollection->serializedData));
     $this->setGetArray(array('id' => $gameCollection->id));
     $content = $this->runControllerWithNoExceptionsAndGetContent('gamification/default/redeemCollection');
     $this->assertContains('Basketball Collection', $content);
 }
 /**
  * Given an  Item (Either User or Person),  try to find an existing model for each type. If the model does
  * not exist, create it and populate the Item and type. @return models found or created indexed by type.
  * @param Item $person
  * @param $collectionTypes
  * @return array
  * @throws FailedToSaveModelException
  */
 public static function resolvePersonAndAvailableTypes(Item $person, $collectionTypes)
 {
     assert('$person->id > 0');
     assert('$person instanceof Contact || $person instanceof User');
     assert('is_array($collectionTypes)');
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $person->getClassId('Item')));
     $searchAttributeData['structure'] = '1';
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GameCollection');
     $where = RedBeanModelDataProvider::makeWhere('GameCollection', $searchAttributeData, $joinTablesAdapter);
     $models = self::getSubset($joinTablesAdapter, null, null, $where, null);
     $modelsByType = array();
     foreach ($collectionTypes as $type) {
         $modelFound = false;
         foreach ($models as $model) {
             if ($model->type == $type) {
                 $modelsByType[$type] = $model;
                 $modelFound = true;
                 break;
             }
         }
         if (!$modelFound) {
             $gameCollectionRules = GameCollectionRulesFactory::createByType($type);
             $gameCollection = new GameCollection();
             $gameCollection->type = $type;
             $gameCollection->person = $person;
             $gameCollection->serializedData = serialize($gameCollectionRules::makeDefaultData());
             $saved = $gameCollection->save();
             if (!$saved) {
                 throw new FailedToSaveModelException();
             }
             $modelsByType[$type] = $gameCollection;
         }
     }
     return $modelsByType;
 }