Foxtrott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/59052
Change subject: Add method Subobject::addPropertyObjectValue
......................................................................
Add method Subobject::addPropertyObjectValue
This method takes data items for property and value and adds them to the
subobject.
Change-Id: I64ff773c95b35ab77531c830344503206c6e028a
---
M includes/Subobject.php
M tests/phpunit/includes/SubobjectTest.php
2 files changed, 77 insertions(+), 10 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki
refs/changes/52/59052/1
diff --git a/includes/Subobject.php b/includes/Subobject.php
index 8d8e655..65042ca 100644
--- a/includes/Subobject.php
+++ b/includes/Subobject.php
@@ -171,10 +171,32 @@
*
* @since 1.9
*
+ * @param SMWDIProperty $propertyDi
+ * @param SMWDataItem $valueDi
+ */
+ public function addPropertyObjectValue( \SMWDIProperty $propertyDi,
\SMWDataItem $valueDi ) {
+
+ if ( !( $this->semanticData instanceof SMWContainerSemanticData
) ) {
+ throw new MWException( 'The semantic data container is
not initialized' );
+ }
+
+ if ( !$propertyDi->isInverse() ) {
+ $this->semanticData->addPropertyObjectValue(
$propertyDi, $valueDi );
+ } else {
+ $this->errors[] = wfMessage( 'smw_noinvannot'
)->inContentLanguage()->text();
+ }
+ }
+
+ /**
+ * Add property / value to the semantic data container
+ *
+ * @since 1.9
+ *
* @param string $propertyName
* @param string $valueString
*/
public function addPropertyValue( $propertyName, $valueString ) {
+
if ( !( $this->semanticData instanceof SMWContainerSemanticData
) ) {
throw new MWException( 'The semantic data container is
not initialized' );
}
@@ -182,19 +204,20 @@
$propertyDv = SMWPropertyValue::makeUserProperty( $propertyName
);
$propertyDi = $propertyDv->getDataItem();
- if ( $propertyDi instanceof \SMWDIProperty &&
!$propertyDi->isInverse() ) {
- $valueDv = SMWDataValueFactory::newPropertyObjectValue(
$propertyDi, $valueString,
- false, $this->semanticData->getSubject() );
- $this->semanticData->addPropertyObjectValue(
$propertyDi, $valueDv->getDataItem() );
+ if ( $propertyDi instanceof \SMWDIProperty ) {
- // Take note of the error for storage (do this here and
not in storage, thus avoiding duplicates).
- if ( !$valueDv->isValid() ) {
- $this->semanticData->addPropertyObjectValue(
new SMWDIProperty( SMWDIProperty::TYPE_ERROR ),
- $propertyDi->getDiWikiPage() );
+ $valueDv = SMWDataValueFactory::newPropertyObjectValue(
$propertyDi, $valueString, false, $this->semanticData->getSubject() );
+
+ if ( $valueDv->isValid() ) {
+
+ $valueDi = $valueDv->getDataItem();
+ $this->addPropertyObjectValue( $propertyDi,
$valueDi );
+ } else {
+
+ // Take note of the error for storage (do this
here and not in storage, thus avoiding duplicates).
+ $this->semanticData->addPropertyObjectValue(
new SMWDIProperty( SMWDIProperty::TYPE_ERROR ), $propertyDi->getDiWikiPage() );
$this->errors = array_merge( $this->errors,
$valueDv->getErrors() );
}
- } else if ( $propertyDi instanceof \SMWDIProperty &&
$propertyDi->isInverse() ) {
- $this->errors[] = wfMessage( 'smw_noinvannot'
)->inContentLanguage()->text();
} else {
// FIXME Get message object
$this->errors[] = wfMessage(
'smw-property-name-invalid', $propertyName )->inContentLanguage()->text();
diff --git a/tests/phpunit/includes/SubobjectTest.php
b/tests/phpunit/includes/SubobjectTest.php
index 8c8d57a..be35caf 100644
--- a/tests/phpunit/includes/SubobjectTest.php
+++ b/tests/phpunit/includes/SubobjectTest.php
@@ -5,7 +5,9 @@
use SMW\Subobject;
use SMWDIWikiPage;
use SMWDataItem;
+use SMWPropertyValue;
use SMWDataValueFactory;
+use MWException;
use Title;
/**
@@ -137,6 +139,21 @@
)
),
+ // #6 Improper value for wikipage property will raise
error
+ array(
+ 'Foo',
+ array(
+ 'identifier' => 'bar',
+ 'property' => array( 'Foo' => '' )
+ ),
+ array( // Expected results
+ 'name' => 'bar',
+ 'errors' => 1,
+ 'propertyLabel' => 'Has improper value
for',
+ 'propertyValue' => 'Foo',
+ )
+ ),
+
);
}
@@ -236,6 +253,33 @@
}
/**
+ * Test addPropertyValueString() exception
+ *
+ * @dataProvider getDataProvider
+ */
+ public function testAddPropertyObjectValueStringException( $title,
array $setup ) {
+ $this->setExpectedException( 'MWException' );
+
+ $subobject = $this->getSubobject( $title );
+ $diWikiPage = \SMWDIWikiPage::newFromTitle(Title::newFromText(
$title ));
+
+ foreach ( $setup['property'] as $property => $value ){
+ $propertyDv = SMWPropertyValue::makeUserProperty(
$property );
+ $propertyDi = $propertyDv->getDataItem();
+
+ // addPropertyObjectValue only deals with properties,
SMWDIErrors have to be handled elsewhere
+ if ( $propertyDi instanceof \SMWDIProperty ) {
+ $valueDv =
SMWDataValueFactory::newPropertyObjectValue( $propertyDi, $value, false,
$diWikiPage );
+ $valueDi = $valueDv->getDataItem();
+
+ $subobject->addPropertyObjectValue(
$propertyDi, $valueDi );
+ } else {
+ throw new MWException( 'Will not test on
something that is not a property.' );
+ }
+ }
+ }
+
+ /**
* Test getAnonymousIdentifier()
*
* @dataProvider getDataProvider
--
To view, visit https://gerrit.wikimedia.org/r/59052
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I64ff773c95b35ab77531c830344503206c6e028a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Foxtrott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits