12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- <?php
- /**
- * @file
- * Tests for the Synonyms Commerce module.
- */
- /**
- * Test synonyms autocomplete widget for commerce product reference field type.
- */
- class CommerceProductReferenceAutocompleteSynonymsWebTestCase extends AbstractAutocompleteSynonymsWebTestCase {
- /**
- * GetInfo method.
- */
- public static function getInfo() {
- return array(
- 'name' => 'Commerce product reference synonyms autocomplete',
- 'description' => 'Ensure that the "synonym friendly autocomplete" widget works correctly with commerce product reference field type.',
- 'group' => 'Synonyms',
- );
- }
- public function setUp($modules = array()) {
- $modules[] = 'commerce_product_reference';
- $modules[] = 'commerce_product_ui';
- $modules[] = 'synonyms_commerce';
- $this->fields['enabled']['instance']['entity_type'] = 'commerce_product';
- $this->fields['enabled']['instance']['bundle'] = 'product';
- $this->fields['disabled']['instance']['entity_type'] = 'commerce_product';
- $this->fields['disabled']['instance']['bundle'] = 'product';
- $this->behavior_implementation['entity_type'] = $this->fields['enabled']['instance']['entity_type'];
- parent::setUp($modules);
- $this->reference_field = array(
- 'type' => 'commerce_product_reference',
- 'field_name' => 'synonyms_commerce',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'settings' => array(),
- );
- $this->reference_field = field_create_field($this->reference_field);
- $this->reference_instance = array(
- 'field_name' => $this->reference_field['field_name'],
- 'entity_type' => $this->entity_type,
- 'bundle' => $this->bundle,
- 'label' => 'Synonym Commerce Autocomplete',
- 'settings' => array(
- 'referenceable_types' => drupal_map_assoc(array('product')),
- ),
- 'widget' => array(
- 'type' => 'synonyms_commerce_autocomplete',
- ),
- );
- $this->reference_instance = field_create_instance($this->reference_instance);
- $this->reference_instance = field_info_instance($this->reference_instance['entity_type'], $this->reference_instance['field_name'], $this->reference_instance['bundle']);
- }
- protected function createTerms() {
- $name = $this->randomName();
- $product = (object) array(
- 'type' => 'product',
- 'title' => $name,
- 'sku' => 'a' . drupal_strtolower($this->randomName()),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['term1'] = $product;
- $name .= $this->randomName();
- $product = (object) array(
- 'type' => 'product',
- 'title' => $name,
- 'sku' => 'z' . drupal_strtolower($this->randomName()),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['term1_longer_name'] = $product;
- $product = (object) array(
- 'type' => 'product',
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['no_synonyms'] = $product;
- $product = (object) array(
- 'type' => 'product',
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['one_synonym'] = $product;
- $product = (object) array(
- 'type' => 'product',
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- array('value' => $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['two_synonyms'] = $product;
- $name = $this->randomName();
- $product = (object) array(
- 'type' => 'product',
- 'title' => $name,
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $name . $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['name_similar_synonym'] = $product;
- $name = 'similar_synonyms_';
- $product = (object) array(
- 'type' => 'product',
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $name . $this->randomName()),
- array('value' => $name . $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['similar_synonyms'] = $product;
- $name = 'one_term_name_another_synonym_';
- $product = (object) array(
- 'type' => 'product',
- 'title' => $name . $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['name_another_synonym'] = $product;
- $product = (object) array(
- 'type' => 'product',
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $name . $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- commerce_product_save($product);
- $this->terms['synonym_another_name'] = $product;
- $another_product_type = commerce_product_ui_product_type_new();
- $another_product_type['type'] = 'another_vocabulary';
- $another_product_type['name'] = $this->randomName();
- commerce_product_ui_product_type_save($another_product_type);
- $product_similar_product = (object) array(
- 'type' => $another_product_type['type'],
- 'title' => $this->entityLabel($this->terms['no_synonyms']),
- 'sku' => drupal_strtolower($this->randomName()),
- );
- commerce_product_save($product_similar_product);
- $product_similar_synonym = (object) array(
- 'type' => $another_product_type['type'],
- 'title' => $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- 'sku' => drupal_strtolower($this->randomName()),
- );
- commerce_product_save($product_similar_synonym);
- $synonym_similar_product = (object) array(
- 'type' => $another_product_type['type'],
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(LANGUAGE_NONE => array(
- $this->entityLabel($this->terms['no_synonyms']),
- )),
- );
- commerce_product_save($synonym_similar_product);
- $synonym_similar_synonym = (object) array(
- 'type' => $another_product_type['type'],
- 'title' => $this->randomName(),
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(LANGUAGE_NONE => array(
- $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- )),
- );
- commerce_product_save($synonym_similar_synonym);
- }
- }
- /**
- * Test synonyms-friendly select widget for commerce product reference field.
- */
- class CommerceProductReferenceSelectSynonymsWebTestCase extends AbstractSelectSynonymsWebTestCase {
- /**
- * Array of products on which the testing is held.
- *
- * @var array
- */
- protected $products = array();
- public static function getInfo() {
- return array(
- 'name' => 'Commerce product reference synonyms select',
- 'description' => 'Ensure that the "synonym friendly select" widget works correctly with commerce product reference field.',
- 'group' => 'Synonyms',
- );
- }
- public function setUp($modules = array()) {
- $modules[] = 'commerce_product_reference';
- $modules[] = 'synonyms_commerce';
- $this->fields['enabled']['instance']['entity_type'] = 'commerce_product';
- $this->fields['enabled']['instance']['bundle'] = 'product';
- $this->fields['disabled']['instance']['entity_type'] = 'commerce_product';
- $this->fields['disabled']['instance']['bundle'] = 'product';
- $this->behavior_implementation['entity_type'] = $this->fields['enabled']['instance']['entity_type'];
- parent::setUp($modules);
- $this->reference_field = array(
- 'type' => 'commerce_product_reference',
- 'field_name' => 'synonyms_commerce',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'settings' => array(),
- );
- $this->reference_field = field_create_field($this->reference_field);
- $this->reference_instance = array(
- 'field_name' => $this->reference_field['field_name'],
- 'entity_type' => $this->entity_type,
- 'bundle' => $this->bundle,
- 'label' => 'Synonym Commerce Select',
- 'widget' => array(
- 'type' => 'synonyms_commerce_select',
- ),
- );
- $this->reference_instance = field_create_instance($this->reference_instance);
- }
- public function testWidgetSorting() {
- $cardinality = array(
- 1 => 1,
- FIELD_CARDINALITY_UNLIMITED => 'unlimited',
- );
- $required = array(
- TRUE => 'required',
- FALSE => 'not required',
- );
- foreach ($cardinality as $cardinality_k => $cardinality_v) {
- foreach ($required as $required_k => $required_v) {
- $this->reference_field['cardinality'] = $cardinality_k;
- field_update_field($this->reference_field);
- $this->reference_instance['required'] = $required_k;
- field_update_instance($this->reference_instance);
- $options = array();
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- );
- $this->drupalGet('node/add/synonyms-test-content');
- $this->assertSynonymsSelect($options, 'Synonyms select sorting by name works for the cardinality of ' . $cardinality_v . ' and ' . $required_v);
- }
- }
- }
- public function testWidget() {
- $name = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $name,
- $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
- $this->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- $this->terms['product2']->product_id,
- $this->terms['product3']->product_id,
- $this->synonymSelectKey($this->terms['product3'], $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- ),
- ), 'Save');
- $node = $this->drupalGetNodeByTitle($name);
- $this->drupalGet('node/' . $node->nid);
- $this->assertText($this->terms['product1']->title, 'Product is saved when its synonym is submitted through synonyms friendly select for the unlimited cardinality.');
- $this->assertText($this->terms['product2']->title, 'Product is saved when it is submitted through synonyms friendly select for the unlimited cardinality.');
- $this->assertText($this->terms['product3']->title, 'Product is saved only once when the term and its synonym are submitted through synonyms friendly select for the unlimited cardinality.');
- $options = array();
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'selected' => TRUE,
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'selected' => TRUE,
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'selected' => TRUE,
- );
- $this->drupalGet('node/' . $node->nid . '/edit');
- $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with field cardinality more than 1.');
- $this->reference_field['cardinality'] = 2;
- field_update_field($this->reference_field);
- $name = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $name,
- $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
- $this->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- $this->terms['product2']->product_id,
- $this->terms['product3']->product_id,
- ),
- ), 'Save');
- $this->assertText('this field cannot hold more than 2 values.', 'Submitting 3 entries into a field with cardinality of 2, that refer to 3 products, results in a form error.');
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $name,
- $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
- $this->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- $this->terms['product2']->product_id,
- $this->synonymSelectKey($this->terms['product2'], $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- ),
- ), 'Save');
- $node = $this->drupalGetNodeByTitle($name);
- $this->drupalGet('node/' . $node->nid);
- $this->assertText($this->terms['product1']->title, 'Submitting 3 entries into a field with cardinality of 2, that refer to only 2 products, results in form getting submitted. Product #1 is saved.');
- $this->assertText($this->terms['product2']->title, 'Product #2 is saved.');
- $this->reference_field['cardinality'] = 1;
- field_update_field($this->reference_field);
- // We need to invoke this one. Without it some caching within Entity
- // metadata wrappers interferes. See https://www.drupal.org/node/2717019 for
- // details.
- entity_flush_caches();
- $name = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $name,
- $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
- ), 'Save');
- $node = $this->drupalGetNodeByTitle($name);
- $this->drupalGet('node/' . $node->nid);
- $this->assertText($this->terms['product1']->title, 'Product is saved when its synonym is submitted through synonyms friendly select for the cardinality of 1.');
- $options = array();
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'selected' => TRUE,
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- );
- $this->drupalGet('node/' . $node->nid . '/edit');
- $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
- $this->drupalPost('node/' . $node->nid . '/edit', array(
- $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->terms['product2']->product_id,
- ), 'Save');
- $this->drupalGet('node/' . $node->nid);
- $this->assertNoText($this->terms['product1']->title, 'After updating entity the old product is removed.');
- $this->assertText($this->terms['product2']->title, 'Product is saved when it is submitted through synonyms friendly select for the cardinality of 1.');
- $options = array();
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- 'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product1'],
- 'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'selected' => TRUE,
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product2'],
- 'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
- );
- $options[] = array(
- 'entity' => $this->terms['product3'],
- );
- $this->drupalGet('node/' . $node->nid . '/edit');
- $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
- }
- protected function createTerms() {
- $this->terms['product1'] = (object) array(
- 'type' => $this->behavior_implementation['bundle'],
- 'title' => 'Product 1',
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => 'Product 1A' . $this->randomName()),
- array('value' => 'Product 1Z' . $this->randomName()),
- ),
- ),
- );
- commerce_product_save($this->terms['product1']);
- $this->terms['product2'] = (object) array(
- 'type' => $this->behavior_implementation['bundle'],
- 'title' => 'Product 2',
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => 'Product 2Z' . $this->randomName()),
- array('value' => 'Product 2A' . $this->randomName()),
- ),
- ),
- );
- commerce_product_save($this->terms['product2']);
- $this->terms['product3'] = (object) array(
- 'type' => $this->behavior_implementation['bundle'],
- 'title' => 'Product 3',
- 'sku' => drupal_strtolower($this->randomName()),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => 'Another Product 3A' . $this->randomName()),
- array('value' => 'Another Product 3Z' . $this->randomName()),
- ),
- ),
- );
- commerce_product_save($this->terms['product3']);
- }
- }
- /**
- * Test CommerceProductReferenceSynonymsBehavior class.
- */
- class CommerceProductReferenceSynonymsBehaviorWebTestCase extends AbstractSynonymsProviderFieldWebTestCase {
- /**
- * GetInfo method.
- */
- public static function getInfo() {
- return array(
- 'name' => 'CommerceProductReferenceSynonymsBehavior',
- 'description' => 'Ensure that the synonyms module extracts synonyms from commerce product reference fields correctly.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp($modules = array()) {
- $modules[] = 'commerce_product_reference';
- $modules[] = 'commerce_product_ui';
- $modules[] = 'synonyms_commerce';
- $this->fields['enabled']['field'] = array(
- 'field_name' => 'reference',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'type' => 'commerce_product_reference',
- );
- parent::setUp($modules);
- }
- /**
- * Test synonyms extraction for 'commerce_product_reference' field type.
- */
- public function testCommerceProductReference() {
- // Testing synonymsExtract().
- $this->assertSynonymsExtract(array(), array(), 'on empty field.');
- $synonym_entity = $this->createProduct();
- $this->assertSynonymsExtract(array(
- LANGUAGE_NONE => array(
- 0 => array(
- 'product_id' => entity_id('commerce_product', $synonym_entity),
- ),
- ),
- ), array(entity_label('commerce_product', $synonym_entity)), 'on a field that holds one value.');
- // Testing mergeEntityAsSynonym() method.
- $product = $this->createProduct();
- $this->assertMergeEntityAsSynonym(array(), $product, 'commerce_product', array(array('product_id' => $product->product_id)), 'on a product.');
- // Testing synonymFind() method.
- $this->assertSynonymsFind(array(), db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on empty field');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on a field without values');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- ),
- ),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on a field with a value but searching for another string');
- $meta_data = array();
- $synonym_entity = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity)),
- ),
- ),
- 'found_synonyms' => array(entity_label('commerce_product', $synonym_entity)),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity)), 'on a field with a single value searching for that string');
- $meta_data = array();
- $synonym_entity = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity)),
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- ),
- ),
- 'found_synonyms' => array(entity_label('commerce_product', $synonym_entity)),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity)), 'on a field with 2 values searching for one of those 2 values');
- $meta_data = array();
- $synonym_entity = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity)),
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- ),
- ),
- 'found_synonyms' => array(entity_label('commerce_product', $synonym_entity)),
- );
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- ),
- ),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity)), 'on 2 fields with 2 values each searching for one of those values');
- $meta_data = array();
- $synonym_entity = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity)),
- ),
- ),
- 'found_synonyms' => array(entity_label('commerce_product', $synonym_entity)),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr(entity_label('commerce_product', $synonym_entity), 1, -1)) . '%', 'LIKE'), 'on a field with a value searching for a string LIKE the %value%');
- $meta_data = array();
- $tag = $this->randomName();
- $synonym_entity1 = $this->createProduct($tag . $this->randomName());
- $synonym_entity2 = $this->createProduct($tag . $this->randomName());
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity1)),
- array('product_id' => entity_id('commerce_product', $synonym_entity2)),
- ),
- ),
- 'found_synonyms' => array(
- entity_label('commerce_product', $synonym_entity1),
- entity_label('commerce_product', $synonym_entity2),
- ),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, db_like($tag) . '%', 'LIKE'), 'on a field with 2 similar values searching a string like %both values%');
- $meta_data = array();
- $synonym_entity1 = $this->createProduct();
- $synonym_entity2 = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity1)),
- array('product_id' => entity_id('commerce_product', $synonym_entity2)),
- ),
- ),
- 'found_synonyms' => array(
- entity_label('commerce_product', $synonym_entity1),
- entity_label('commerce_product', $synonym_entity2),
- ),
- );
- $condition = db_or()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity1))
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity2));
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for value1 or value2');
- $meta_data = array();
- $synonym_entity = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity)),
- array('product_id' => entity_id('commerce_product', $this->createProduct())),
- ),
- ),
- 'found_synonyms' => array(entity_label('commerce_product', $synonym_entity)),
- );
- $condition = db_and()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity))
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr(entity_label('commerce_product', $synonym_entity), 1, -1)) . '%', 'LIKE');
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for value1 and LIKE value1%');
- $meta_data = array();
- $synonym_entity1 = $this->createProduct();
- $synonym_entity2 = $this->createProduct();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity1)),
- array('product_id' => entity_id('commerce_product', $synonym_entity2)),
- ),
- ),
- 'found_synonyms' => array(
- entity_label('commerce_product', $synonym_entity1),
- entity_label('commerce_product', $synonym_entity2),
- ),
- );
- $condition = db_or()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity1))
- ->condition(db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, entity_label('commerce_product', $synonym_entity2))->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr(entity_label('commerce_product', $synonym_entity2), 1, -1)) . '%', 'LIKE'));
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for (value1 or (value2 AND value2%))');
- $meta_data = array();
- $synonym_entity1 = $this->createProduct($this->randomName() . ' ' . $this->randomName() . ' ' . $this->randomName());
- $synonym_entity2 = $this->createProduct(str_replace(' ', '-', entity_label('commerce_product', $synonym_entity1)));
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('product_id' => entity_id('commerce_product', $synonym_entity1)),
- array('product_id' => entity_id('commerce_product', $synonym_entity2)),
- ),
- ),
- 'found_synonyms' => array(
- entity_label('commerce_product', $synonym_entity1),
- entity_label('commerce_product', $synonym_entity2),
- ),
- );
- $condition = db_and()
- ->where("REPLACE(" . AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER . ", ' ', '-') = :synonym", array(
- ':synonym' => entity_label('commerce_product', $synonym_entity2),
- ));
- $this->assertSynonymsFind($meta_data, $condition, "on a field with 2 values, where 2nd value replaces spaces with dashes in the 1st value, searching for REPLACE(column, ' ', '-') = value2");
- }
- /**
- * Supportive function.
- *
- * Create an entity of necessary entity type (in our test it's commerce
- * product).
- *
- * @param string $label
- * Label to use for the entity that is about to be created
- * @param string $bundle
- * Bundle to use for the entity that is about to be created
- *
- * @return object
- * Fully loaded entity object of the just created entity
- */
- protected function createProduct($label = NULL, $bundle = 'product') {
- if (is_null($label)) {
- $label = $this->randomName();
- }
- $entity = commerce_product_new($bundle);
- $entity->title = $label;
- $entity->sku = drupal_html_id($label);
- commerce_product_save($entity);
- return $entity;
- }
- }
- /**
- * Test CommercePriceSynonymsBehavior class.
- */
- class CommercePriceSynonymsBehaviorWebTestCase extends AbstractSynonymsProviderFieldWebTestCase {
- /**
- * GetInfo method.
- */
- public static function getInfo() {
- return array(
- 'name' => 'CommercePriceSynonymsBehavior',
- 'description' => 'Ensure that the synonyms module extracts synonyms from commerce price fields correctly.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp($modules = array()) {
- $modules[] = 'commerce_price';
- $modules[] = 'commerce_product_reference';
- $modules[] = 'commerce_product_ui';
- $modules[] = 'synonyms_commerce';
- $this->fields['enabled']['field'] = array(
- 'field_name' => 'synonyms_commerce_price',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'type' => 'commerce_price',
- );
- parent::setUp($modules);
- }
- /**
- * Test synonyms extraction for 'commerce_price' field type.
- */
- public function testCommercePrice() {
- // Testing synonymsExtract().
- $this->assertSynonymsExtract(array(), array(), 'on empty field.');
- $this->assertSynonymsExtract(array(
- LANGUAGE_NONE => array(
- 0 => array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- ),
- ), array(commerce_currency_format(1000, 'USD')), 'on a field that holds one value.');
- // Testing synonymFind() method.
- $this->assertSynonymsFind(array(), db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on empty field');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on a field without values');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this->randomName()), 'on a field with a value but searching for another string');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(commerce_currency_format(1000, 'USD')),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000), 'on a field with a single value searching for that string');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 2000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(commerce_currency_format(1000, 'USD')),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000), 'on a field with 2 values searching for one of those 2 values');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 2000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(commerce_currency_format(1000, 'USD')),
- );
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 100,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 200,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000), 'on 2 fields with 2 values each searching for one of those values');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(commerce_currency_format(1000, 'USD')),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(10) . '%', 'LIKE'), 'on a field with a value searching for a string LIKE the %value%');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 100,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(
- commerce_currency_format(1000, 'USD'),
- commerce_currency_format(100, 'USD'),
- ),
- );
- $this->assertSynonymsFind($meta_data, db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, db_like(100) . '%', 'LIKE'), 'on a field with 2 similar values searching a string like %both values%');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 2000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(
- commerce_currency_format(1000, 'USD'),
- commerce_currency_format(2000, 'USD'),
- ),
- );
- $condition = db_or()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000)
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 2000);
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for value1 or value2');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 2000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(commerce_currency_format(1000, 'USD')),
- );
- $condition = db_and()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000)
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(10) . '%', 'LIKE');
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for value1 and LIKE value1%');
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'amount' => 1000,
- 'currency_code' => 'USD',
- ),
- array(
- 'amount' => 2000,
- 'currency_code' => 'USD',
- ),
- ),
- ),
- 'found_synonyms' => array(
- commerce_currency_format(1000, 'USD'),
- commerce_currency_format(2000, 'USD'),
- ),
- );
- $condition = db_or()
- ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 1000)
- ->condition(db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 2000)->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(20) . '%', 'LIKE'));
- $this->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for (value1 or (value2 AND value2%))');
- }
- }
|