MediaWiki  master
MutableRevisionSlotsTest.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
17 
22  protected function newRevisionSlots( $slots = [] ) {
23  return new MutableRevisionSlots( $slots );
24  }
25 
26  public function provideConstructorFailue() {
27  yield 'array or the wrong thing' => [
28  [ 1, 2, 3 ]
29  ];
30  }
31 
39  public function testConstructorFailue( $slots ) {
40  $this->setExpectedException( InvalidArgumentException::class );
41 
42  new MutableRevisionSlots( $slots );
43  }
44 
45  public function testSetMultipleSlots() {
46  $slots = new MutableRevisionSlots();
47 
48  $this->assertSame( [], $slots->getSlots() );
49 
50  $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
51  $slots->setSlot( $slotA );
52  $this->assertTrue( $slots->hasSlot( 'some' ) );
53  $this->assertSame( $slotA, $slots->getSlot( 'some' ) );
54  $this->assertSame( [ 'some' => $slotA ], $slots->getSlots() );
55 
56  $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
57  $slots->setSlot( $slotB );
58  $this->assertTrue( $slots->hasSlot( 'other' ) );
59  $this->assertSame( $slotB, $slots->getSlot( 'other' ) );
60  $this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() );
61  }
62 
64  $slots = new MutableRevisionSlots();
65 
66  $this->assertSame( [], $slots->getSlots() );
67 
69  $slots->setSlot( $slotA );
70  $this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) );
71  $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
72 
74  $slots->setSlot( $slotB );
75  $this->assertSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) );
76  $this->assertSame( [ 'main' => $slotB ], $slots->getSlots() );
77  }
78 
84  private function newSavedSlot( $role, Content $content ) {
85  return SlotRecord::newSaved( 7, 7, 'xyz', SlotRecord::newUnsaved( $role, $content ) );
86  }
87 
88  public function testInheritSlotOverwritesSlot() {
89  $slots = new MutableRevisionSlots();
91  $slots->setSlot( $slotA );
92  $slotB = $this->newSavedSlot( SlotRecord::MAIN, new WikitextContent( 'B' ) );
93  $slotC = $this->newSavedSlot( 'foo', new WikitextContent( 'C' ) );
94  $slots->inheritSlot( $slotB );
95  $slots->inheritSlot( $slotC );
96  $this->assertSame( [ 'main', 'foo' ], $slots->getSlotRoles() );
97  $this->assertNotSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) );
98  $this->assertNotSame( $slotC, $slots->getSlot( 'foo' ) );
99  $this->assertTrue( $slots->getSlot( SlotRecord::MAIN )->isInherited() );
100  $this->assertTrue( $slots->getSlot( 'foo' )->isInherited() );
101  $this->assertSame( $slotB->getContent(), $slots->getSlot( SlotRecord::MAIN )->getContent() );
102  $this->assertSame( $slotC->getContent(), $slots->getSlot( 'foo' )->getContent() );
103  }
104 
106  $slots = new MutableRevisionSlots();
107 
108  $this->assertSame( [], $slots->getSlots() );
109 
111  $slots->setSlot( $slotA );
112  $this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) );
113  $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
114 
115  $newContent = new WikitextContent( 'B' );
116  $slots->setContent( SlotRecord::MAIN, $newContent );
117  $this->assertSame( $newContent, $slots->getContent( SlotRecord::MAIN ) );
118  }
119 
120  public function testRemoveExistingSlot() {
122  $slots = new MutableRevisionSlots( [ $slotA ] );
123 
124  $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
125 
126  $slots->removeSlot( SlotRecord::MAIN );
127  $this->assertSame( [], $slots->getSlots() );
128  $this->setExpectedException( RevisionAccessException::class );
129  $slots->getSlot( SlotRecord::MAIN );
130  }
131 
132  public function testNewFromParentRevisionSlots() {
134  $parentSlots = [
135  'some' => $this->newSavedSlot( 'some', new WikitextContent( 'X' ) ),
136  'other' => $this->newSavedSlot( 'other', new WikitextContent( 'Y' ) ),
137  ];
138  $slots = MutableRevisionSlots::newFromParentRevisionSlots( $parentSlots );
139  $this->assertSame( [ 'some', 'other' ], $slots->getSlotRoles() );
140  $this->assertNotSame( $parentSlots['some'], $slots->getSlot( 'some' ) );
141  $this->assertNotSame( $parentSlots['other'], $slots->getSlot( 'other' ) );
142  $this->assertTrue( $slots->getSlot( 'some' )->isInherited() );
143  $this->assertTrue( $slots->getSlot( 'other' )->isInherited() );
144  $this->assertSame( $parentSlots['some']->getContent(), $slots->getContent( 'some' ) );
145  $this->assertSame( $parentSlots['other']->getContent(), $slots->getContent( 'other' ) );
146  }
147 
148 }
static newSaved( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
Constructs a complete SlotRecord for a newly saved revision, based on the incomplete proto-slot...
Definition: SlotRecord.php:164
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
static newFromParentRevisionSlots(array $slots)
Constructs a MutableRevisionSlots that inherits from the given list of slots.
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
Mutable version of RevisionSlots, for constructing a new revision.
$content
Definition: pageupdater.txt:72