You are here

public function CommerceProductReferenceSynonymsBehaviorWebTestCase::testCommerceProductReference in Synonyms 7

Test synonyms extraction for 'commerce_product_reference' field type.

File

synonyms_commerce/synonyms_commerce.test, line 632
Tests for the Synonyms Commerce module.

Class

CommerceProductReferenceSynonymsBehaviorWebTestCase
Test CommerceProductReferenceSynonymsBehavior class.

Code

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");
}