示例#1
0
 /**
  * Changing a group's metadata should change its ETag
  */
 public function testUpdateMetadata()
 {
     $response = API::userGet(self::$config['userID'], "groups?content=json&key=" . self::$config['apiKey']);
     $this->assert200($response);
     // Get group API URI and ETag
     $xml = API::getXMLFromResponse($response);
     $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
     $xml->registerXPathNamespace('zapi', 'http://zotero.org/ns/api');
     $groupID = (string) array_shift($xml->xpath("//atom:entry/zapi:groupID"));
     $url = (string) array_shift($xml->xpath("//atom:entry/atom:link[@rel='self']/@href"));
     $url = str_replace(self::$config['apiURLPrefix'], '', $url);
     $etag = (string) array_shift($xml->xpath("//atom:entry/atom:content/@etag"));
     // Make sure format=etags returns the same ETag
     $response = API::userGet(self::$config['userID'], "groups?format=etags&key=" . self::$config['apiKey']);
     $this->assert200($response);
     $json = json_decode($response->getBody());
     $this->assertEquals($etag, $json->{$groupID});
     // Update group metadata
     $json = json_decode(array_shift($xml->xpath("//atom:entry/atom:content")));
     $xml = new SimpleXMLElement("<group/>");
     foreach ($json as $key => $val) {
         switch ($key) {
             case 'id':
             case 'members':
                 continue;
             case 'name':
                 $name = "My Test Group " . uniqid();
                 $xml['name'] = $name;
                 break;
             case 'description':
                 $description = "This is a test description " . uniqid();
                 $xml->{$key} = $description;
                 break;
             case 'url':
                 $urlField = "http://example.com/" . uniqid();
                 $xml->{$key} = $urlField;
                 break;
             default:
                 $xml[$key] = $val;
         }
     }
     $xml = trim(preg_replace('/^<\\?xml.+\\n/', "", $xml->asXML()));
     $response = API::put($url, $xml, array("Content-Type: text/xml"), array("username" => self::$config['rootUsername'], "password" => self::$config['rootPassword']));
     $this->assert200($response);
     $xml = API::getXMLFromResponse($response);
     $xml->registerXPathNamespace('zxfer', 'http://zotero.org/ns/transfer');
     $group = $xml->xpath('//atom:entry/atom:content/zxfer:group');
     $this->assertCount(1, $group);
     $this->assertEquals($name, $group[0]['name']);
     $response = API::userGet(self::$config['userID'], "groups?format=etags&key=" . self::$config['apiKey']);
     $this->assert200($response);
     $json = json_decode($response->getBody());
     $newETag = $json->{$groupID};
     $this->assertNotEquals($etag, $newETag);
     // Check ETag header on individual group request
     $response = API::groupGet($groupID, "?content=json&key=" . self::$config['apiKey']);
     $this->assert200($response);
     $this->assertEquals($newETag, $response->getHeader('ETag'));
     $json = json_decode(API::getContentFromResponse($response));
     $this->assertEquals($name, $json->name);
     $this->assertEquals($description, $json->description);
     $this->assertEquals($urlField, $json->url);
 }