Esempio n. 1
0
 /**
  * @dataProvider instanceProvider
  */
 public function testToArrayMore(Diff $diffOp)
 {
     $array = $diffOp->toArray();
     $this->assertArrayHasKey('operations', $array);
     $this->assertInternalType('array', $array['operations']);
     $this->assertArrayHasKey('isassoc', $array);
     $this->assertTrue(is_bool($array['isassoc']) || is_null($array['isassoc']), 'The isassoc element needs to be a boolean or null');
 }
 /**
  * @dataProvider provideConstruction
  *
  * @param EntityDiff $entityDiff
  * @param Diff $redirectDiff
  */
 public function testConstruction(EntityDiff $entityDiff, Diff $redirectDiff)
 {
     $diff = new EntityContentDiff($entityDiff, $redirectDiff);
     $this->assertArrayEquals($entityDiff->getOperations(), $diff->getEntityDiff()->getOperations());
     $this->assertEmpty(array_diff(array_keys($entityDiff->getOperations()), array_keys($diff->getEntityDiff()->getOperations())));
     $this->assertArrayEquals($redirectDiff->getOperations(), $diff->getRedirectDiff()->getOperations());
     $this->assertEmpty(array_diff(array_keys($redirectDiff->getOperations()), array_keys($diff->getRedirectDiff()->getOperations())));
 }
 /**
  * @param EntityDiff $entityDiff
  * @param Diff $redirectDiff
  */
 public function __construct(EntityDiff $entityDiff, Diff $redirectDiff)
 {
     $operations = array();
     $this->entityDiff = $entityDiff;
     $this->redirectDiff = $redirectDiff;
     $operations = array_merge($operations, $this->entityDiff->getOperations());
     $operations = array_merge($operations, $this->redirectDiff->getOperations());
     parent::__construct($operations, true);
 }
Esempio n. 4
0
 /**
  * @param DiffOp[] $operations
  */
 public function __construct(array $operations = array())
 {
     $this->fixSubstructureDiff($operations, 'aliases');
     $this->fixSubstructureDiff($operations, 'label');
     $this->fixSubstructureDiff($operations, 'description');
     $this->fixSubstructureDiff($operations, 'claim');
     parent::__construct($operations, true);
 }
Esempio n. 5
0
 /**
  * @see Patcher::patch
  *
  * Applies the provided diff to the provided array and returns the result.
  * The provided diff needs to be non-associative. In other words, calling
  * isAssociative on it should return false.
  *
  * Note that remove operations can introduce gaps into the input array $base.
  * For instance, when the input is [ 0 => 'a', 1 => 'b', 2 => 'c' ], and there
  * is one remove operation for 'b', the result will be [ 0 => 'a', 2 => 'c' ].
  *
  * @since 0.4
  *
  * @param array $base
  * @param Diff $diff
  *
  * @return array
  * @throws PatcherException
  */
 public function patch(array $base, Diff $diff)
 {
     if ($diff->looksAssociative()) {
         $this->handleError('ListPatcher can only patch using non-associative diffs');
     }
     foreach ($diff as $diffOp) {
         if ($diffOp instanceof DiffOpAdd) {
             $base[] = $diffOp->getNewValue();
         } elseif ($diffOp instanceof DiffOpRemove) {
             $key = array_search($diffOp->getOldValue(), $base, true);
             if ($key === false) {
                 $this->handleError('Cannot remove an element from a list if it is not present');
                 continue;
             }
             unset($base[$key]);
         }
     }
     return $base;
 }
 /**
  * Generates and returns an HTML visualization of the provided redirect Diff.
  *
  * @since 0.5
  *
  * @param Diff $diff
  *
  * @return string
  */
 protected function visualizeRedirectDiff(Diff $diff)
 {
     if ($diff->isEmpty()) {
         return '';
     }
     //TODO: localize path (keys in the diff array)
     $linkDiffVisualizer = new DiffView(array(), $diff, $this->siteStore, $this->entityIdFormatter, $this->context);
     $html = $linkDiffVisualizer->getHtml();
     return $html;
 }
Esempio n. 7
0
 /**
  * @dataProvider getApplicableDiffProvider
  *
  * @param Diff $diff
  * @param array $currentObject
  * @param Diff $expected
  * @param string|null $message
  */
 public function testGetApplicableDiff(Diff $diff, array $currentObject, Diff $expected, $message = null)
 {
     $patcher = new ListPatcher();
     $actual = $patcher->getApplicableDiff($currentObject, $diff);
     $this->assertEquals($expected->getOperations(), $actual->getOperations(), $message);
 }
Esempio n. 8
0
 public function testEmptyElementsInRecursiveDiff()
 {
     $old = array('en' => array('a' => 'en-foo', 'b' => 'en-bar'));
     $new = array('en' => array('a' => 'en-foo', 'b' => 'en-bar'));
     $differ = new MapDiffer(true);
     $diff = new Diff($differ->doDiff($old, $new));
     $this->assertTrue($diff->isEmpty());
     $this->assertTrue($diff->getOperations() === array());
 }
 /**
  * @param Diff $redirectPatch
  *
  * @return EntityRedirect|null
  */
 private function getPatchedRedirect(Diff $redirectPatch)
 {
     // See getRedirectData() for the structure of the data array.
     $redirData = $this->getRedirectData();
     if (!$redirectPatch->isEmpty()) {
         $patcher = new MapPatcher();
         $redirData = $patcher->patch($redirData, $redirectPatch);
     }
     if (isset($redirData['redirect'])) {
         /* @var EntityHandler $handler */
         $handler = $this->getContentHandler();
         $entityId = $this->getEntityId();
         $targetId = $handler->makeEntityId($redirData['redirect']);
         return new EntityRedirect($entityId, $targetId);
     } else {
         return null;
     }
 }
Esempio n. 10
0
 /**
  * @dataProvider notEqualsProvider
  */
 public function testNotEquals(Diff $diff, Diff $target)
 {
     $this->assertFalse($diff->equals($target));
     $this->assertFalse($target->equals($diff));
 }
 /**
  * Returns an array structure suitable for building an edit summary for the respective
  * change to site links.
  *
  * @param string $action e.g. 'remove', see the constants in EntityChange
  * @param Diff $siteLinkDiff The change's site link diff
  * @param Title|null $title The page we create an edit summary for
  *
  * @return array|null
  */
 private function getSiteLinkMessage($action, Diff $siteLinkDiff, Title $title = null)
 {
     if ($siteLinkDiff->isEmpty()) {
         return null;
     }
     //TODO: Implement comments specific to the affected page.
     //       Different pages may be affected in different ways by the same change.
     //       Also, merged changes may affect the same page in multiple ways.
     $diffOps = $siteLinkDiff->getOperations();
     $siteId = $this->siteId;
     // change involved site link to client wiki
     if (array_key_exists($siteId, $diffOps)) {
         // $siteLinkDiff changed from containing atomic diffs to
         // containing map diffs. For B/C, handle both cases.
         $diffOp = $diffOps[$siteId];
         if ($diffOp instanceof Diff) {
             if (array_key_exists('name', $diffOp)) {
                 $diffOp = $diffOp['name'];
             } else {
                 // change to badges only, use original message
                 return null;
             }
         }
         $params = $this->getSiteLinkAddRemoveParams($diffOp, $action, $siteId, $title);
     } else {
         $diffOpCount = count($diffOps);
         if ($diffOpCount === 1) {
             $params = $this->getSiteLinkChangeParams($diffOps);
         } else {
             // multiple changes, use original message
             return null;
         }
     }
     return $params;
 }
Esempio n. 12
0
 private function patchMapOrList(array $base, Diff $diff)
 {
     if ($diff->looksAssociative()) {
         $base = $this->patch($base, $diff);
     } else {
         $base = $this->listPatcher->patch($base, $diff);
     }
     return $base;
 }