Exemple #1
0
		public void LoadTree_SkipsRootWhenExcluded()
		{
			var serializedRootItem = new ProxyItem();
			var predicate = CreateExclusiveTestPredicate();
			var logger = Substitute.For<ISerializationLoaderLogger>();
			var loader = CreateTestLoader(predicate: predicate, logger: logger);

			TestLoadTree(loader, serializedRootItem);

			logger.Received().SkippedItemPresentInSerializationProvider(serializedRootItem, Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
		}
Exemple #2
0
		public void LoadTree_LoadsRootWhenIncluded()
		{
			var serializedRootItem = new ProxyItem();
			var evaluator = Substitute.For<IEvaluator>();
			var sourceData = Substitute.For<ISourceDataStore>();
			sourceData.GetByPathAndId(serializedRootItem.Path, serializedRootItem.Id, serializedRootItem.DatabaseName).Returns((IItemData)null);

			var loader = CreateTestLoader(evaluator: evaluator, sourceDataStore: sourceData);

			TestLoadTree(loader, serializedRootItem);

			evaluator.Received().EvaluateNewSerializedItem(serializedRootItem);
		}
Exemple #3
0
		public void LoadTree_LoadsChildOfRootWhenIncluded()
		{
			var dataStore = Substitute.For<ITargetDataStore>();
			var root = new ProxyItem();
			var child = new ProxyItem { ParentId = root.Id };
			dataStore.GetChildren(root).Returns(new[] { child });

			var evaluator = Substitute.For<IEvaluator>();
			var loader = CreateTestLoader(evaluator: evaluator, targetDataStore: dataStore);

			TestLoadTree(loader, root);

			evaluator.Received().EvaluateUpdate(Arg.Any<IItemData>(), child);
		}
		public void ShouldExcludeChildrenExcludedByPredicate()
		{
			var child = new ProxyItem();
			var parent = new ProxyItem();
			parent.SetProxyChildren(new[] { child });

			var predicate = Substitute.For<IPredicate>();
			predicate.Includes(child).Returns(new PredicateResult(false));

			Assert.NotEmpty(parent.GetChildren());

			var filtered = new PredicateFilteredItemData(parent, predicate);

			Assert.Empty(filtered.GetChildren());
		}
Exemple #5
0
		public void LoadTree_SkipsChildOfRootWhenExcluded()
		{
			var dataStore = Substitute.For<ITargetDataStore>();
			var root = new ProxyItem();
			var child = new ProxyItem { ParentId = root.Id };

			dataStore.GetChildren(root).Returns(new[] { child });

			var predicate = CreateExclusiveTestPredicate(new[] { root });
			var logger = Substitute.For<ISerializationLoaderLogger>();
			var loader = CreateTestLoader(predicate: predicate, logger: logger, targetDataStore: dataStore);

			TestLoadTree(loader, root);

			logger.Received().SkippedItemPresentInSerializationProvider(Arg.Is<IItemData>(data => data.Id == child.Id), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
		}
		private void RunItemChangeTest(Action<ProxyItem> setup, Action<Item> assert)
		{
			using (var db = new Db())
			{
				var deserializer = CreateTestDeserializer(db);

				var itemId = AddExistingItem(db);

				var itemData = new ProxyItem(new ItemData(db.GetItem(itemId)));

				setup(itemData);

				deserializer.Deserialize(itemData);

				var fromDb = db.GetItem(itemId);

				Assert.NotNull(fromDb);
				assert(fromDb);
			}
		}
		public void Deserialize_IgnoresField_ExcludedWithFieldFilter()
		{
			var ignoredFieldId = ID.NewID;

			var fieldFilter = Substitute.For<IFieldFilter>();
			fieldFilter.Includes(Arg.Any<Guid>()).Returns(true);
			fieldFilter.Includes(ignoredFieldId.Guid).Returns(false);

			var deserializer = new DefaultDeserializer(Substitute.For<IDefaultDeserializerLogger>(), fieldFilter);
			deserializer.ParentDataStore = new SitecoreDataStore(deserializer);

			using (var db = new Db())
			{
				var itemId = ID.NewID;

				db.Add(new DbItem("Test Item", itemId)
				{
					{ignoredFieldId, "Test Value"}
				});

				var itemData = new ProxyItem(new ItemData(db.GetItem(itemId)));

				var fields = new List<IItemFieldValue>();
				fields.Add(new FakeFieldValue("Changed Ignored Value", fieldId: ignoredFieldId.Guid));
				((ProxyItemVersion)itemData.Versions.First()).Fields = fields;

				deserializer.Deserialize(itemData);

				var fromDb = db.GetItem(itemId);

				Assert.Equal(fromDb[ignoredFieldId], "Test Value");
			}
		}
        protected virtual void WriteItem(IItemData item, string path)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNullOrEmpty(path, "path");

            var proxiedItem = new ProxyItem(item);

            lock (FileUtil.GetFileLock(path))
            {
                try
                {
                    _treeWatcher.PushKnownUpdate(path);
                    var directory = Path.GetDirectoryName(path);
                    if (directory != null && !Directory.Exists(directory)) Directory.CreateDirectory(directory);

                    using (var writer = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        _formatter.WriteSerializedItem(proxiedItem, writer);
                    }
                }
                catch (Exception exception)
                {
                    if (File.Exists(path)) File.Delete(path);
                    throw new SfsWriteException("Error while writing SFS item " + path, exception);
                }
            }

            AddToMetadataCache(item, path);
            _dataCache.AddOrUpdate(path, proxiedItem);
        }
        public void RemoveVersions(ItemDefinition itemDefinition, Language language, bool removeSharedData, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");

            var sourceItem = GetSourceFromIdIfIncluded(itemDefinition);

            if (sourceItem == null) return;

            // create a clone of the item to remove the versions from
            var versionRemovingProxy = new ProxyItem(sourceItem);

            // drop all versions in the language
            versionRemovingProxy.Versions = versionRemovingProxy.Versions.Where(v => !v.Language.Equals(language.CultureInfo));

            SerializeItemIfIncluded(versionRemovingProxy, "Versions Removed");
        }
        public void RemoveVersion(ItemDefinition itemDefinition, VersionUri version, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");

            var sourceItem = GetSourceFromIdIfIncluded(itemDefinition);

            if (sourceItem == null) return; // predicate excluded item

            // create a clone of the item to remove the version from
            var versionRemovingProxy = new ProxyItem(sourceItem);

            // exclude the removed version
            versionRemovingProxy.Versions = versionRemovingProxy.Versions.Where(v => !v.Language.Equals(version.Language.CultureInfo) || !v.VersionNumber.Equals(version.Version.Number));

            SerializeItemIfIncluded(versionRemovingProxy, "Version Removed");
        }
        public void CreateItem(ItemDefinition newItem, ID templateId, ItemDefinition parent, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(newItem, "itemDefinition");

            // get the item's parent. We need to know the path to be able to serialize it, and the ItemDefinition doesn't have it.
            var parentItem = Database.GetItem(parent.ID);

            Assert.IsNotNull(parentItem, "New item parent {0} did not exist!", parent.ID);

            // create a new skeleton item based on the ItemDefinition; at this point the item has no fields, data, etc
            var newItemProxy = new ProxyItem(newItem.Name, newItem.ID.Guid, parent.ID.Guid, newItem.TemplateID.Guid, parentItem.Paths.Path + "/" + newItem.Name, Database.Name);
            newItemProxy.BranchId = newItem.BranchId.Guid;

            // serialize the skeleton if the predicate includes it
            SerializeItemIfIncluded(newItemProxy, "Created");
        }
        public void AddVersion(ItemDefinition itemDefinition, VersionUri baseVersion, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");

            var sourceItem = GetSourceFromIdIfIncluded(itemDefinition);

            if (sourceItem == null) return; // not an included item

            // we make a clone of the item so that we can insert a new version on it
            var versionAddProxy = new ProxyItem(sourceItem);

            // determine what the next version number should be in the current language
            // (highest number currently present + 1)
            var newVersionNumber = 1 + versionAddProxy.Versions
                .Where(v => v.Language.Equals(baseVersion.Language.CultureInfo))
                .Select(v => v.VersionNumber)
                .DefaultIfEmpty()
                .Max();

            IItemVersion newVersion;

            // if the base version is 0 or less, that means add a blank version (per SC DP behavior). If 1 or more we should copy all fields on that version into the new version.
            if (baseVersion.Version.Number > 0)
            {
                newVersion = versionAddProxy.Versions.First(v => v.Language.Equals(baseVersion.Language.CultureInfo) && v.VersionNumber.Equals(baseVersion.Version.Number));
                newVersion = new ProxyItemVersion(newVersion) { VersionNumber = newVersionNumber }; // creating a new proxyversion essentially clones the existing version
            }
            else newVersion = new ProxyItemVersion(baseVersion.Language.CultureInfo, newVersionNumber);

            // inject the new version we created into the proxy item
            var newVersions = versionAddProxy.Versions.Concat(new[] { newVersion }).ToArray();
            versionAddProxy.Versions = newVersions;

            // flush to serialization data store
            SerializeItemIfIncluded(versionAddProxy, "Version Added");
        }
        public virtual void CopyItem(ItemDefinition source, ItemDefinition destination, string copyName, ID copyId, CallContext context)
        {
            if (DisableSerialization) return;

            // copying is easy - all we have to do is serialize the item using the copyID and new target destination as the parent.
            // Copied children will all result in multiple calls to CopyItem so we don't even need to worry about them.
            var existingItem = GetSourceFromId(source.ID, true);

            Assert.IsNotNull(existingItem, "Existing item to copy was not in the database!");

            var destinationItem = GetSourceFromId(destination.ID, true);

            Assert.IsNotNull(destinationItem, "Copy destination was not in the database!");

            // wrap the existing item in a proxy so we can mutate it into a copy
            var copyTargetItem = new ProxyItem(existingItem);

            copyTargetItem.Path = $"{destinationItem.Path}/{copyName}";
            copyTargetItem.Name = copyName;
            copyTargetItem.Id = copyId.Guid;
            copyTargetItem.ParentId = destination.ID.Guid;

            if (!_predicate.Includes(copyTargetItem).IsIncluded) return; // destination parent is not in a path that we are serializing, so skip out

            _targetDataStore.Save(copyTargetItem);
            _logger.CopiedItem(_targetDataStore.FriendlyName, existingItem, copyTargetItem);
        }
        public void ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changeList, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, nameof(itemDefinition));
            Assert.ArgumentNotNull(changeList, nameof(changeList));

            var sourceItem = GetSourceFromId(itemDefinition.ID);

            var existingSerializedItem = _targetDataStore.GetByPathAndId(sourceItem.Path, sourceItem.Id, sourceItem.DatabaseName);

            if (existingSerializedItem == null) return;

            var newItem = new ProxyItem(sourceItem); // note: sourceItem gets dumped. Because it has field changes made to it.
            newItem.TemplateId = changeList.Target.ID.Guid;

            _targetDataStore.Save(newItem);

            _logger.SavedItem(_targetDataStore.FriendlyName, sourceItem, "TemplateChanged");
        }
		public void ShouldReturnExpectedValue()
		{
			var fake = new ProxyItem { Path = "/sitecore/content/test item", DatabaseName = "master" };

			Assert.Equal("master:/sitecore/content/test item (" + fake.Id + ")", fake.GetDisplayIdentifier());
		}