/** * Instantiates the encryption utility object so that components depending * on {@link EncryptedFieldsBehavior} can also be instantiated. */ public function cryptInit() { if (!$this->_cryptInit) { $key = $this->owner->basePath . '/config/encryption.key'; $iv = $this->owner->basePath . '/config/encryption.iv'; if (extension_loaded('openssl') && extension_loaded('mcrypt') && file_exists($key) && file_exists($iv)) { EncryptedFieldsBehavior::setup($key, $iv); } else { // Use unsafe method with encryption EncryptedFieldsBehavior::setupUnsafe(); } } }
/** * Override that uses a specific key/iv for unit testing */ public static function setUpBeforeClass() { parent::setUpBeforeClass(); EncryptedFieldsBehavior::setupUnsafe(); }
/** * Tests transparent transition between encrypted and unencrypted */ public function testUnsafe() { $cred = $this->credentials('testUser'); $expectedAttributes = $cred->auth->attributes; EncryptedFieldsBehavior::setupUnsafe(); $cred->save(); $unencryptedJSON = Yii::app()->db->createCommand()->select('auth')->from('x2_credentials')->where('id=:id', array(':id' => $cred->id))->queryScalar(); $attributes = CJSON::decode($unencryptedJSON); $this->assertEquals($expectedAttributes, $attributes); }