Example #1
0
 /**
  * @dataProvider provideData
  */
 public function testBotEdits($params, $expected)
 {
     $this->doLogin('wbbot');
     // -- do the request --------------------------------------------------
     if (array_key_exists('handle', $params)) {
         $params['id'] = EntityTestHelper::getId($params['handle']);
         unset($params['handle']);
     }
     // wbmergeitems needs special treatment as it takes two entities
     if ($params['action'] === 'wbmergeitems') {
         $params['fromid'] = EntityTestHelper::getId($params['fromid']);
         $params['toid'] = EntityTestHelper::getId($params['toid']);
     }
     list($result, , ) = $this->doApiRequestWithToken($params, null, self::$users['wbbot']->getUser());
     // -- check the result ------------------------------------------------
     $this->assertArrayHasKey('success', $result, "Missing 'success' marker in response.");
     $this->assertResultHasEntityType($result);
     if ($params['action'] !== 'wbmergeitems') {
         $this->assertArrayHasKey('entity', $result, "Missing 'entity' section in response.");
         $this->assertArrayHasKey('lastrevid', $result['entity'], 'entity should contain lastrevid key');
         $myid = $result['entity']['id'];
     } else {
         $this->assertArrayHasKey('from', $result, "Missing 'from' section in response.");
         $myid = $result['from']['id'];
     }
     // -- get the recentchanges -------------------------------------------
     $rcRequest = array('action' => 'query', 'list' => 'recentchanges', 'rcprop' => 'title|flags', 'rctoponly' => '1', 'rclimit' => 5);
     //@todo this really makes this test slow, is there a better way?
     $rcResult = $this->doApiRequest($rcRequest, null, false, self::$users['wbbot']->getUser());
     // -- check the recent changes result ---------------------------------
     $this->assertArrayHasKey('query', $rcResult[0], "Must have a 'query' key in the result from the API");
     $this->assertArrayHasKey('recentchanges', $rcResult[0]['query'], "Must have a 'recentchanges' key in 'query' subset of the result from the API");
     //NOTE: the order of the entries in recentchanges is undefined if multiple
     //      edits were done in the same second.
     $change = null;
     $entityNamespaceLookup = WikibaseRepo::getDefaultInstance()->getEntityNamespaceLookup();
     $itemNs = $entityNamespaceLookup->getEntityNamespace(CONTENT_MODEL_WIKIBASE_ITEM);
     foreach ($rcResult[0]['query']['recentchanges'] as $rc) {
         $title = Title::newFromText($rc['title']);
         // XXX: strtoupper is a bit arcane, would ne nice to have a utility function for prefixed id -> title.
         if ($title->getNamespace() == $itemNs && $title->getText() === strtoupper($myid)) {
             $change = $rc;
             break;
         }
     }
     $this->assertNotNull($change, 'no change matching ID ' . $myid . ' found in recentchanges feed!');
     $this->assertResultValue($expected, 'new', $change);
     $this->assertResultValue($expected, 'bot', $change);
 }
 /**
  * @dataProvider invalidRequestProvider
  */
 public function testInvalidRequest($itemHandle, $guid, $propertyHande, $snakType, $value, $error)
 {
     $itemId = new ItemId(EntityTestHelper::getId($itemHandle));
     $item = WikibaseRepo::getDefaultInstance()->getEntityLookup()->getEntity($itemId);
     $propertyId = EntityTestHelper::getId($propertyHande);
     if ($guid === null) {
         /** @var Item $item */
         $statements = $item->getStatements()->toArray();
         /** @var Statement $statement */
         $statement = reset($statements);
         $guid = $statement->getGuid();
     }
     if (!is_string($value)) {
         $value = json_encode($value);
     }
     $params = array('action' => 'wbsetqualifier', 'claim' => $guid, 'property' => $propertyId, 'snaktype' => $snakType, 'value' => $value);
     try {
         $this->doApiRequestWithToken($params);
         $this->fail('Invalid request did not raise an error');
     } catch (UsageException $ex) {
         $this->assertEquals($error, $ex->getCodeString(), 'Invalid request raised correct error');
     }
 }
 /**
  * Applies self::$idMap to all data in the given data structure, recursively.
  *
  * @param mixed &$data
  */
 protected function injectIds(&$data)
 {
     EntityTestHelper::injectIds($data, self::$idMap);
 }
 /**
  * @dataProvider provideData
  */
 public function testSetSiteLink(array $params, array $expected)
 {
     // -- set any defaults ------------------------------------
     if (array_key_exists('handle', $params)) {
         $params['id'] = EntityTestHelper::getId($params['handle']);
         unset($params['handle']);
     }
     $params['action'] = 'wbsetsitelink';
     // Replace the placeholder item ids in the API params
     if (isset($params['badges'])) {
         $params['badges'] = str_replace(array('{gaItem}', '{faItem}', '{otherItem}'), array(self::$gaItemId->getSerialization(), self::$faItemId->getSerialization(), self::$otherItemId->getSerialization()), $params['badges']);
     }
     // -- do the request --------------------------------------------------
     list($result, , ) = $this->doApiRequestWithToken($params);
     //@todo all of the below is very similar to the code in ModifyTermTestCase
     //This might be able to go in the same place
     // Replace the placeholder item ids in the expected results... this sucks
     if (is_array($expected['value'])) {
         $expected['value'] = $this->expectionPlaceholder($expected['value']);
     }
     // -- check the result ------------------------------------------------
     $this->assertArrayHasKey('success', $result, "Missing 'success' marker in response.");
     $this->assertResultHasEntityType($result);
     $this->assertArrayHasKey('entity', $result, "Missing 'entity' section in response.");
     $this->assertArrayHasKey('lastrevid', $result['entity'], 'entity should contain lastrevid key');
     // -- check the result only has our changed data (if any)  ------------
     $linkSite = $params['linksite'];
     $siteLinks = $result['entity']['sitelinks'];
     $this->assertEquals(1, count($siteLinks), "Entity return contained more than a single site");
     $this->assertArrayHasKey($linkSite, $siteLinks, "Entity doesn't return expected site");
     $siteLink = $siteLinks[$linkSite];
     $this->assertEquals($linkSite, $siteLink['site'], "Returned incorrect site");
     if (array_key_exists($linkSite, $expected['value'])) {
         $expectedSiteLink = $expected['value'][$linkSite];
         $this->assertArrayHasKey('url', $siteLink);
         $this->assertEquals($expectedSiteLink['title'], $siteLink['title'], "Returned incorrect title");
         $this->assertArrayHasKey('badges', $siteLink);
         $this->assertArrayEquals($expectedSiteLink['badges'], $siteLink['badges']);
     } elseif (empty($expected['value'])) {
         $this->assertArrayHasKey('removed', $siteLink, "Entity doesn't return expected 'removed' marker");
     }
     // -- check any warnings ----------------------------------------------
     if (array_key_exists('warning', $expected)) {
         $this->assertArrayHasKey('warnings', $result, "Missing 'warnings' section in response.");
         $this->assertEquals($expected['warning'], $result['warnings']['messages']['0']['name']);
         $this->assertArrayHasKey('html', $result['warnings']['messages']);
     }
     // -- check item in database -------------------------------------------
     $dbEntity = $this->loadEntity($result['entity']['id']);
     $expectedInDb = count($expected['value']);
     if (array_key_exists('indb', $expected)) {
         $expectedInDb = $expected['indb'];
     }
     $this->assertArrayHasKey('sitelinks', $dbEntity);
     $this->assertCount($expectedInDb, $dbEntity['sitelinks']);
     $this->assertContainsAllSiteLinks($expected['value'], $dbEntity['sitelinks']);
     // -- check the edit summary --------------------------------------------
     if (!array_key_exists('warning', $expected) || $expected['warning'] != 'edit-no-change') {
         $this->assertRevisionSummary(array('wbsetsitelink', $params['linksite']), $result['entity']['lastrevid']);
         if (array_key_exists('summary', $params)) {
             $this->assertRevisionSummary("/{$params['summary']}/", $result['entity']['lastrevid']);
         }
     }
 }
 /**
  * @dataProvider invalidRequestProvider
  */
 public function testInvalidRequest($itemHandle, $guid, $referenceValue, $referenceHash, $error)
 {
     $itemId = new ItemId(EntityTestHelper::getId($itemHandle));
     $item = WikibaseRepo::getDefaultInstance()->getEntityLookup()->getEntity($itemId);
     if ($guid === null) {
         /** @var Item $item */
         $statements = $item->getStatements()->toArray();
         /** @var Statement $statement */
         $statement = reset($statements);
         $guid = $statement->getGuid();
     }
     $prop = new PropertyId(EntityTestHelper::getId('StringProp'));
     $snak = new PropertyValueSnak($prop, new StringValue($referenceValue));
     $reference = new Reference(new SnakList(array($snak)));
     $serializedReference = $this->serializeReference($reference);
     $params = array('action' => 'wbsetreference', 'statement' => $guid, 'snaks' => json_encode($serializedReference['snaks']), 'snaks-order' => json_encode($serializedReference['snaks-order']));
     if ($referenceHash) {
         $params['reference'] = $referenceHash;
     }
     try {
         $this->doApiRequestWithToken($params);
         $this->fail('Invalid request did not raise an error');
     } catch (UsageException $ex) {
         $this->assertEquals($error, $ex->getCodeString(), 'Invalid request raised correct error');
     }
 }
 /**
  * @param string $handle
  *
  * @return null|Title
  */
 protected function getTestEntityTitle($handle)
 {
     try {
         $wikibaseRepo = WikibaseRepo::getDefaultInstance();
         $idString = EntityTestHelper::getId($handle);
         $id = $wikibaseRepo->getEntityIdParser()->parse($idString);
         $title = $wikibaseRepo->getEntityTitleLookup()->getTitleForId($id);
     } catch (OutOfBoundsException $ex) {
         $title = null;
     }
     return $title;
 }
 /**
  * @dataProvider provideMergeItemsPermissions
  */
 public function testMergeItems(array $permissions = null, $expectedError)
 {
     $params = array('fromid' => EntityTestHelper::getId('Oslo'), 'toid' => EntityTestHelper::getId('Empty'));
     $this->doPermissionsTest('wbmergeitems', $params, $permissions, $expectedError);
 }