/** * @param string $index * @param array $fields * @param Dimension[] $dimensions * @return void */ public function create($index, array $fields, array $dimensions = []) { $this->createFulltextIndex($this->indexScopeResolver->resolve($index, $dimensions)); if ($fields) { $this->createFlatIndex($this->flatScopeResolver->resolve($index, $dimensions), $fields); } }
/** * Retrieve customer IDs for reindex * * @return array */ protected function getCustomerIdsForReindex() { $connection = $this->resource->getConnection(); $gridTableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []); $select = $connection->select()->from($this->resource->getTableName($gridTableName), 'last_visit_at')->order('last_visit_at DESC')->limit(1); $lastVisitAt = $connection->query($select)->fetchColumn(); $select = $connection->select()->from($this->resource->getTableName('customer_log'), 'customer_id')->where('last_login_at > ?', $lastVisitAt); $customerIds = []; foreach ($connection->query($select)->fetchAll() as $row) { $customerIds[] = $row['customer_id']; } return $customerIds; }
public function testCreateWithEmptyFields() { $fields = [['name' => 'fieldName1', 'type' => 'fieldType1', 'size' => 'fieldSize1'], ['name' => 'fieldName2', 'type' => 'fieldType2', 'size' => 'fieldSize2'], ['name' => 'fieldName3', 'type' => 'fieldType3', 'size' => 'fieldSize3'], ['name' => 'fieldName3', 'dataType' => 'varchar', 'type' => 'text', 'size' => '255'], ['name' => 'fieldName3', 'dataType' => 'mediumtext', 'type' => 'text', 'size' => '16777216'], ['name' => 'fieldName3', 'dataType' => 'text', 'type' => 'text', 'size' => '65536']]; $index = 'index_name'; $expectedTable = 'index_name_scope3_scope5_scope1'; $dimensions = ['index_name_scope_3' => $this->createDimensionMock('scope', 3), 'index_name_scope_5' => $this->createDimensionMock('scope', 5), 'index_name_scope_1' => $this->createDimensionMock('scope', 1)]; $position = 0; $this->indexScopeResolver->expects($this->once())->method('resolve')->with($index, $dimensions)->willReturn($expectedTable); $this->flatScopeResolver->expects($this->once())->method('resolve')->with($index, $dimensions)->willReturn($index . '_flat'); $position = $this->mockFulltextTable($position, $expectedTable, true); $this->mockFlatTable($position, $index . '_flat'); $this->target->create($index, $fields, $dimensions); }
public function testFilter() { $attributeCode = 'attribute-code'; $attributeCodeTwo = 'attribute-code2'; $tableName = 'customer_grid_flat'; $attributes = [$attributeCode => [AttributeMetadataInterface::ATTRIBUTE_CODE => $attributeCode, AttributeMetadataInterface::FRONTEND_INPUT => 'input', AttributeMetadataInterface::FRONTEND_LABEL => 'Frontend label', AttributeMetadataInterface::BACKEND_TYPE => 'static', AttributeMetadataInterface::OPTIONS => [], AttributeMetadataInterface::IS_USED_IN_GRID => true, AttributeMetadataInterface::IS_VISIBLE_IN_GRID => true, AttributeMetadataInterface::IS_FILTERABLE_IN_GRID => true, AttributeMetadataInterface::IS_SEARCHABLE_IN_GRID => true], $attributeCodeTwo => [AttributeMetadataInterface::ATTRIBUTE_CODE => $attributeCodeTwo, AttributeMetadataInterface::FRONTEND_INPUT => 'input', AttributeMetadataInterface::FRONTEND_LABEL => 'Frontend label two', AttributeMetadataInterface::BACKEND_TYPE => 'static', AttributeMetadataInterface::OPTIONS => [], AttributeMetadataInterface::IS_USED_IN_GRID => false, AttributeMetadataInterface::IS_VISIBLE_IN_GRID => false, AttributeMetadataInterface::IS_FILTERABLE_IN_GRID => false, AttributeMetadataInterface::IS_SEARCHABLE_IN_GRID => false]]; $this->indexerRegistry->expects($this->once())->method('get')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID)->willReturn($this->indexer); $this->indexer->expects($this->once())->method('getState')->willReturn($this->indexerState); $this->indexerState->expects($this->once())->method('getStatus')->willReturn(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID); $this->flatScopeResolver->expects($this->once())->method('resolve')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID, [])->willReturn($tableName); $this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->connection->expects($this->once())->method('describeTable')->with($tableName)->willReturn(['attribute-code' => ['Attribute data']]); $this->assertArrayNotHasKey($attributeCodeTwo, $this->model->filter($attributes)); }
/** * @param array $attributes * @return array */ public function filter(array $attributes) { $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); if ($indexer->getState()->getStatus() != StateInterface::STATUS_VALID) { $tableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []); $columns = $this->resource->getConnection()->describeTable($tableName); foreach (array_keys($attributes) as $attributeCode) { if (!isset($columns[$attributeCode])) { unset($attributes[$attributeCode]); } } } return $attributes; }
public function testCreate() { $index = 'index'; $fields = [['type' => 'searchable', 'name' => 'field', 'dataType' => 'int']]; $tableName = 'index_table'; $idxName = 'idxName'; $table = $this->getMockBuilder('Magento\\Framework\\DB\\Ddl\\Table')->disableOriginalConstructor()->getMock(); $this->flatScopeResolver->expects($this->once())->method('resolve')->with($index, [])->willReturn($tableName); $this->connection->expects($this->once())->method('newTable')->with($tableName)->willReturn($table); $table->expects($this->any())->method('addColumn')->willReturnMap([['entity_id', Table::TYPE_INTEGER, 10, ['unsigned' => true, 'nullable' => false], 'Entity ID'], ['field', Table::TYPE_INTEGER, null]]); $this->connection->expects($this->once())->method('createTable')->with($table); $this->resource->expects($this->once())->method('getIdxName')->with($tableName, ['field'], AdapterInterface::INDEX_TYPE_FULLTEXT)->willReturn($idxName); $table->expects($this->once())->method('addIndex')->with($idxName, ['field'], ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]); $this->object->create($index, $fields); }
public function testSyncCustomerGrid() { $gridTable = 'customer_grid_flat'; $customerLogTable = 'customer_log'; $this->indexerRegistry->expects($this->once())->method('get')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID)->willReturn($this->indexer); $this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->flatScopeResolver->expects($this->once())->method('resolve')->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID, [])->willReturn($gridTable); $this->resource->expects($this->exactly(2))->method('getTableName')->willReturnMap([[$gridTable], [$customerLogTable]]); $this->connection->expects($this->exactly(2))->method('select')->willReturn($this->select); $this->select->expects($this->exactly(2))->method('from')->willReturnSelf(); $this->select->expects($this->once())->method('order')->with('last_visit_at DESC')->willReturnSelf(); $this->select->expects($this->once())->method('limit')->with(1)->willReturnSelf(); $this->connection->expects($this->atLeastOnce())->method('query')->with($this->select)->willReturn($this->queryResult); $this->queryResult->expects($this->once())->method('fetchColumn')->willReturn('2015-08-13 10:36:44'); $this->select->expects($this->once())->method('where')->with('last_login_at > ?', '2015-08-13 10:36:44')->willReturnSelf(); $this->queryResult->expects($this->once())->method('fetchAll')->willReturn([['customer_id' => 23], ['customer_id' => 65]]); $this->indexer->expects($this->once())->method('reindexList')->with(['23', '65']); $this->observer->syncCustomerGrid(); }
/** * @param string $index * @param array $fields * @param Dimension[] $dimensions * @return void */ public function create($index, array $fields, array $dimensions = []) { $this->createFlatTable($this->flatScopeResolver->resolve($index, $dimensions), $fields); }